Pytest - Fixture(4) - 测试用例调用fixture

发布时间 2023-05-26 17:16:14作者: 粥雨

Pytest - 测试用例调用fixture

  • 写了fixture方法之后,如果没有配置自动运行,测试用例不去调用,那么fixture是不会运行的;

  • 使用fixture的方式就两种:手动调用、自动运行;


函数传参方式

  • 将fixture装饰的函数名称,作为测试用例函数输入参数

    test_py.py

    import pytest
    
    # 编写fixture@pytest.fixture
    def login():
        print("\n输入账号,密码先登录")
    
    # 调用login的fixture函数
    def test_s1(login):
        print("\n用例test_s1:调用了login函数,进行账户登录操作")
    
    # 不调用login函数,不会运行
    def test_s2():
        print("\n用例test_s2:直接操作")
    
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
        
    

  • 可以传多个fixture参数,先执行的放前面后执行的放后面

    test_py.py

    import pytest
    
    # 编写fixture
    @pytest.fixture
    def logIn():
        print("\nLogIn:输入账号,密码先登录")
    
    # 编写fixture
    @pytest.fixture
    def logOut():
        print("\nLogOut:退出登录")
        
    # 先调用login,后调用logout
    def test_s1(logIn, logOut):
        print("\n用例test_s1:先调用login,后调用logout")
            
    # 先调用logout,后调用login
    def test_s2(logOut, logIn):
        print("\n用例test_s2:先调用logout,后调用login")
    
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
        
    

  • fixture有返回值,必须用fixture函数名称传参的方式。

    • fixture内写了一些方法,例如获取测试数据、传递参数等,

    • 需要将fixture处理好的内容传给测试用例,可以使用yield进行传递;

    test_py.py

    import pytest
    
    @pytest.fixture
    def login():
        print("\n输入账号,密码先登录")
    
        # fixture 返回值,返回abc
        text = "abc"
        yield text
    
    # 测试用例调用,带返回值的fixture
    def test_s1(login):
        print("\n用例test_s1:创建")
        print("输出fixture返回值:" + login)
    
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
        
    

测试用例加装饰器

  • 类上面加装饰器 @pytest.mark.usefixtures() ,代表这个类里面,所有测试用例都会调用该 fixture

    test_py.py

    import pytest
    
    # 编写fixture
    @pytest.fixture
    def login():
        print("\n输入账号,密码先登录")
        
    # 测试类调用fixture
    @pytest.mark.usefixtures("login")
    class Test_py:
        def test_s1(self):
            print("\n用例test_s1:创建")
            
        def test_s2(self):
    		print("\n用例test_s2:删除")
    
            
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
    
    

  • 函数上面加 @pytest.mark.usefixtures() ,代表这个测试用例调用该 fixture

    test_py.py

    import pytest
    
    @pytest.fixture
    def login():
        print("\n输入账号,密码先登录")
    
    # 测试用例调用fixture
    @pytest.mark.usefixtures("login")
    def test_s1():
        print("\n用例test_s1:调用了login函数,进行账户登录操作")
    
    # 测试用例没有调用fixture
    def test_s2():
        print("\n用例test_s2:直接操作")
    
    
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
        
    

  • 可以叠加多个fixture @pytest.mark.usefixtures() 先执行的放下面后执行的放上面

    test_py.py

    import pytest
    
    @pytest.fixture
    def login():
        print("\n====先执行的fixture放下面====")
        print("输入账号,密码先登录")
    
    @pytest.fixture
    def login_cookie():
        print("\n****后执行的fixture放上面****")
        print("获取账号cookie\n")
        
    # 叠加多个fixture
    @pytest.mark.usefixtures("login_cookie")
    @pytest.mark.usefixtures("login")
    def test_s1():
        print("\n用例test_s1:创建")
        
        
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
        
    

自动调用(autouse)

  • fixture 配置参数 autouse=True 后,测试用例不调用fixture,运行时会自动运行fixture 函数。

  • 这样在有很多用例需要调用fixfure时,不需要每条用例都去传入;

    test_py.py

    import pytest
    
    # 配置自动运行的fixture
    @pytest.fixture(scope="function", autouse=True)
    def login():
        print("\n输入账号,密码先登录")
        
    # 测试用例不调用fixture
    def test_s1():
        print("\n用例test_s1:创建")
    
    def test_s2():
        print("\n用例test_s2:删除")
    	
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])