一种使用 pytest 向每个测试添加测试特定参数的方法 [英] A way to add test specific params to each test using pytest
本文介绍了一种使用 pytest 向每个测试添加测试特定参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 pytest 中进行自动化测试,我正在寻找一种方法来从特定于测试的配置文件中读取参数并将其添加到适当的测试中.
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.
例如我希望我的 config.ini 文件看起来像这样:
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?
推荐答案
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]))
您可以像上面一样创建一个 conftest.py 文件,然后您可以在 pytest 的测试文件中使用这些值.
You can create a file conftest.py like as above and you can use those values in your test file of pytest.
这篇关于一种使用 pytest 向每个测试添加测试特定参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文