pytest [email protected]()

Introduction to several major functions of
pytest : @pytest.yield_fixture()
@pytest.mark.xfail()
@pytest.mark.skipif

(1)@pytest.fixture()【Mainly use two parameters: scope\autouse】

@pytest.fixture function definition: Fixture
decorator is used to mark fixed factory functions, which will be activated and executed first when other functions, modules, classes or the entire project call it, and are usually used to complete preset processing and repeated operations

Fixture (scope='function', params=None, autouse=False, ids=None, name=None), source code interpretation:
scope default fuction, there are 4 levels: function, class, module, package/session.
Autouse is turned off by default, fixture(autouse=True): By default, the decorated function is executed in each test case.
params is a formal parameter, you can pass in multiple parameter
names, which is less used, and will not be interpreted temporarily.

@pytest.fixture function 3 major functions:
Function 1: Decoration function _ directly look at the call of test_case_8 in the code, the function marked by the fixture, the function name can be put into the function to be executed as an input parameter. The current function will execute the marked function first. Similar to class inheritance.

# coding=utf-8
import pytest

@pytest.fixture()
def test_case_3():
    print('---3号用例完成---')

@pytest.fixture()
def test_case_4():
    print('---4号用例完成---')

@pytest.fixture()
def test_case_5():
    print('---5号用例完成---')

@pytest.fixture()
def test_case_6():
    print('---6号用例完成---')

@pytest.fixture()
def test_case_7():
    print('---7号用例完成---')

@pytest.fixture()
def test_case_8():
    print('---8号用例完成---')

# (1)这里按照【从下到上的顺序】,执行优先级是3、4、5
@pytest.mark.usefixtures('test_case_5')
@pytest.mark.usefixtures('test_case_4')
@pytest.mark.usefixtures('test_case_3')
class Testlogin001:

    # 被pytest.fixture()装饰的函数,函数名可以作为变量传递给测试用例,最终在执行测试用例之前执行这个装饰过的函数
    def test_case_1(self, test_case_8):
        print('---1号用例完成---')

    # (2)这里按照调用了前面的函数test_case_6,局部的调用,执行优先级是最高的。
    @pytest.mark.usefixtures('test_case_7')
    @pytest.mark.usefixtures('test_case_6')
    def test_case_2(self):
        print('---2号用例完成---')


if __name__ == "__main__":
    pytest.main(['-vs', 'test_1.py'])
    
test_1.py::Testlogin001::test_case_1 
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---8号用例完成---
---1号用例完成---
PASSED
退出浏览器

Function 2: Automatically execute the marked function. [Functions marked by fixtures are not automatically executed by default ]
Here are generally placed in the conftest.py file. Before the test case is executed, each test case will automatically start the browser and automatically enter the interface of the current module.

import pytest
 @pytest.fixture(autouse=True) # 设置为默认运行
 def before():
     print("------->before")
     
 class Test_ABC:
     def setup(self):
         print("------->setup")
     def test_a(self):
         print("------->test_a")
         assert 1
         
 if __name__ == '__main__':
     pytest.main("-s  test_abc.py")

conftest.py file

# coding=utf-8
import pytest
from selenium import webdriver
import logging

logger = logging.getLogger()

@pytest.fixture(autouse=True)
def start_module():
    print('---进入要执行模块的的界面---')

Function 3: Set the scope

The fixture function is used in conjunction with the conftest.py file. Use scenarios:
@pytest.fixture() The decorated function is executed once before each test case [If you want to restart the browser for each test case, enter the For the test interface, use this]
@pytest.fixture(scope='class') The decorated function is executed once before the class of the test case.
@pytest.fixture(scope='modlue') The decorated function is executed once before the py file.
@pytest.fixture(scope='package') The decorated function is executed once in the current directory.

Guess you like

Origin blog.csdn.net/weixin_45451320/article/details/113278303