问题
I'm working on automated testing in pytest, and i'm looking for a way to read params from a config file that are specific to a test and add it to the appropriate test.
for example I would like my config.ini file to look like this:
[Driver]
#some genral variables
[Test_exmpl1]
#variables that I would like to use in Test_exmpl1
username= exmp@gmail.com
password= 123456
[Test_exmpl2]
#variables that I would like to use in Test_exmpl2
username= exmp2@gmail.com
password= 123456789
Now in the code I would like to be able to use these params in the correct test:
class Test_exmpl1(AppiumTestCase):
def test_on_board(self):
self.view = Base_LoginPageObject()
# view = BaseLoginPageObject
self.view = self.view.login(config.username, config.password)
#config.username =exmp@gmail.com
#config.password = 123456
class Test_exmpl2(AppiumTestCase):
def test_on_board(self):
self.view = Base_LoginPageObject()
# view = BaseLoginPageObject
self.view = self.view.login(config.username, config.password)
#config.username =exmp2@gmail.com
#config.password = 123456789
Does anyone have an idea how I should go about doing that?
回答1:
conftest.py
@pytest.fixture()
def before(request):
print("request.cls name is :-- ",request.cls.__name__)
if request.cls.__name__ == 'Test_exmpl1':
return["username","password"]
elif request.cls.__name__ == 'Test_exmpl2':
return["username2","password2"]
test_module.py
import pytest
class Test_exmpl1():
def test_on_board(self,before):
print("IN CLASS 1")
print("username :-- %s and password is %s"%(before[0],before[1]))
class Test_exmpl2():
def test_on_board(self,before):
print("IN CLASS 2")
print("username :-- %s and password is %s"%(before[0],before[1]))
You can create a file conftest.py like as above and you can use those values in your test file of pytest.
来源:https://stackoverflow.com/questions/46947994/a-way-to-add-test-specific-params-to-each-test-using-pytest