fixtures used to run before or after test case.
@pytest.fixture()
def setup():
print("will be executed first")
def test_fixturedemo(setup):
print("i will execute steps in fixturedemo method")
We can change above fixture, to run after test also, whatenver we write after yield keyword will execute after test.
@pytest.fixture()
def setup():
print("will be executed first")
yield
print("will be executed last")
conftext.py --> file to declare fixtures, so it will be available to all test files in that specific folder.
instead of passing argument of fixture name to every test case.
we can wrap all the test case methods in class and declare fixture at class level
wrapper fixture class:
---------
import pytest
@pytest.mark.usefixtures("setup")
class TestExample:
def test_fixtureDemo(self):
print("fixtureDemo method")
def test_fixtureDemo1(self):
print("fixtureDemo1 method")
def test_fixtureDemo2(self):
print("fixtureDemo2 method")
instead of running a fixture with each and every test case, to run only once for a set.
conftest.py
--
import pytest
@pytest.fixture(scope="class")
def setup():
print("will be executing first before first test case")
yield
print("will be executed last after last test case")
data driven fixtures to load data into tests
-----
@pytest.fixture()
def dataLoad():
print("user profile data is being created")
return ["Rahul","Shetty","rahulshettyacademy.com"]
test_fixturesData.py
-------
import pytest
@pytest.mark.usefixtures("dataLoad")
class TestExample2
def test_editProfile(self,dataLoad): ##when we are returning something from fixture, we need to pass the fixture name as argument,which contains the data.
print(dataLoad)
print(dataLoad[0])
print(dataLoad[1])
send multiple data using fixtures, parameters
data driven and paramaeterization can be donw with return statements in tuple format
it will run once before class is initiated and at the end
## conftest.py @pytest.fixture(params=["chrome","Firefox","IE"]) ##will send one value for each run,for 3 times def crossBrowser(request): return request.param @pytest.fixture(params=[("chrome","Rahul","shetty"),("Firefox","Rahul"),"IE"]) ##to pass multiple values as a tuple def crossBrowser1(request): return request.param
sample testing: test_fixturesData.py
import pytest def test_crossBrowser(crossBrowser): print(crossBrowser) def test_crossBrowser1(crossBrowser1): print(crossBrowser1) print(crossBrowser1[0])
No comments:
Post a Comment