Proficient in automation, Pytest automated testing framework - the front and rear (implementation) of fixture use cases


foreword

There are many ways to realize the front and back of the test case. When actually writing test scripts, choose according to the actual situation

1. xunit type

def setup_function()
def teardown_function()

def setup_method(self)
def teardown_method(self)

@classmethod
def setup_class(cls)
@classmethod
def teardown_class(cls)

2. unittest type

#先要在class类中继承unittest.testcase
@classmethod
def setUpClass(cls)
@classmethod
def tearDownClass(cls)
def setup(self)
def tearDown(self)

3. The fixture type in pytest
defines the fixture

@pytest.fixture
def fixture_func():
    print('前置条件')
    yield  #这里可以有返回值,用于夹具的继承
    print('后置条件')
@pytest.fixture(scope,autouse)

The values ​​of scope are:
function Default scope, function scope, end after the test [similar to setup/teardown]
class end after the last test in the class [similar to setupClass/teardownClass]
module end after the last test in the module package end after the last
test in the package
session end after the last test in a session ends [a pytest is called a session]

The default value of autouse is False,
which needs to be added at the module level/package level/session level

use fixture

@pytest.mark.usefixtures("fixture_func")
def test():
    print('开始测试')

Shared fixtures
The shared fixtures are all placed in the conftest.py file;
it will start from the test file and search for the conftest.py file layer by layer;
the test case can directly call the fixtures in the shared fixture without additional import;

#conftest.py文件,在该文件下的夹具为共享夹具
@pytest.fixture(scope='class')
def driver(pytestconfig):
    browser = pytestconfig.getoption('--browser')
    if browser == 'edge':
        s = Service(executable_path=settings.BROWSER_DRIVERS['edge'])
        with webdriver.Edge(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
     if browser == 'chrome':
        s = Service(executable_path=settings.BROWSER_DRIVERS['chrome'])
        with webdriver.Chrome(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
     if browser == 'firefox':
        s = Service(executable_path=settings.BROWSER_DRIVERS['firefox'])
        with webdriver.Firefox(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd

4. Execution steps for multiple use of fixtures

Execute the ones with a large scope first, and then execute the ones with a small scope at
the same level, and call the fixtures in sequence

5. Inheritance of fixtures

It can only be inherited from a large range or the same range.
After the fixture is inherited, you can get the return value from the fixture

example:

#conftest.py 共享文件
@pytest.fixture(scope='class')
def driver(pytestconfig):
    browser = pytestconfig.getoption('--browser')
    if browser == 'edge':
        s = Service(executable_path=settings.BROWSER_DRIVERS['edge']) #webdriver存放路径需要自己修改
        with webdriver.Edge(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd      #这里有返回值,用于夹具的继承
    if browser == 'chrome':
        s = Service(executable_path=settings.BROWSER_DRIVERS['chrome'])
        with webdriver.Chrome(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
    if browser == 'firefox':
        s = Service(executable_path=settings.BROWSER_DRIVERS['firefox'])
        with webdriver.Firefox(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
			
#测试用例文件
class TestMaxWindow():
    def test_maxWindow(self, driver):
      	#执行步骤:
        #1.会在共享夹具中找到driver,yield之前的为前置操作
        #2.执行到yield,夹具有返回值则得到参数
        #3.在该测试用例中可以用夹具返回的参数
        #4.测试用例结束后,继续执行夹具中的后置操作
        driver.maximize_window()
The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

No matter how difficult you encounter, you must maintain firm belief and courage to move forward. Only by constantly striving can you surpass yourself, create your own brilliant life, and let your dreams illuminate the way forward.

On the road of pursuing dreams, don't be afraid of difficulties and setbacks, keep fighting, believe in your own abilities and potentials, and only by making continuous efforts can you create your own brilliance and bloom the most beautiful light in life.

No matter how tortuous and difficult the road ahead is, the heart of perseverance will never be extinguished. Believe in your own strength and potential, and bravely pursue your dreams. Only by working hard can you create your own brilliant life and achieve a great future.

Guess you like

Origin blog.csdn.net/x2waiwai/article/details/131854017