一种使用pytest为每个测试添加测试特定参数的方法

时间:2017-10-26 07:16:59

标签: python pytest

我正在使用pytest进行自动化测试,我正在寻找一种从配置文件读取特定于测试的params的方法,并将其添加到相应的测试中。

例如,我希望我的config.ini文件看起来像这样:

    [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

现在在代码中我希望能够在正确的测试中使用这些参数:

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

有没有人知道我应该怎么做呢?

1 个答案:

答案 0 :(得分:0)

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的测试文件中使用这些值。

相关问题