Confitest. In pytest Usage of PY

  • preface
  • What is confitest py
  • conftest. Py features
  • conftest. Py usage
  • conftest. Py actual case
  • test_ Baidu directory


In the previous article on fixture, we used confitest Py file, then conf test How to use the PY file? Let’s learn more about confitest The characteristics and usage of Py file

What is confitest py

We learned about fixtures before. Fixtures can be directly defined in test scripts, but sometimes we want a fixture to be reused, which requires centralized management of fixtures and pytest to use filesconftest.pyCentrally manage firmware In a complex project, you can define confitest. Com at different directory levels Py, whose scope is the directory and subdirectory where it is located. Usually,conftest.pyand@pytest.fixture()Will be used in combination to achieve global pre-processing and post-processing.

conftest. Py features

  • conftest.pyThe file name is fixed and cannot be modified
  • conftest.pyIt should be in the same pakage as the running use case and have__init__.pyfile
  • unwantedimportImportconftest.pyFile. The pytest example will automatically identify the file. If it is placed in the root directory, it can be called in the global directory. If it is placed in a package, it will be valid in the package
  • Different directories can have their own confitest Py, there can be more than one in a
  • Pytest will read by defaultconftest.pyAll fixtures and test files in the same directory will be executed before runningconftest.pyfile

conftest. Py usage

In our actual test, conf test Py file needs to be used in combination with fixture, so the parameter scope in fixture is also applicable to confitest The feature of fixture in py is explained here

  • The scope parameter of fixture in confitest is session, and all test files are executed once before (after) executionconftest.pyThe fixture in the file.
  • The scope parameter of fixture in confitest is module, and each test The PY file will be executed once before (after) executionconftest.pyFixture in file
  • The scope parameter of fixture in confitest is class, and the test class in each test file will be executed once before (after) executionconftest.pyFixture in file
  • The scope parameter of fixture in confitest is function, and the test cases of all files will be executed once before (after) executionconftest.pyFixture in file

conftest. Py actual case

Let’s create a new project according to this directory

In the root directory confitestdemo

Confitest. In the root directory Py files are generally written with global fixtures, such as login

import pytest

def login():
    Print ("* * * login succeeded, return user name * * *")
    name = "rockche"
    yield name
    Print ("* * * logout * * *")

def get_name(login):
    name = login
    Print (F "-- each use case calls the outer layer fixture: print user name: {name} --")

Test cases under the root directory

def test_get_name(login):
    name = login
    Print ("* * * basic use case: get user name * * *")
    Print (F "user name: {name}")

Run all use cases under confirm demo

import pytest

if __name__ == '__main__':
    pytest.main(["-s", "../conftestdemo/"])

test_ Baidu directory

Configure the unique fixture for the test case of Baidu website

import pytest

def open_baidu(login):
    name = login
    Print (F "user {name} opens Baidu")

def test_case2_01(open_baidu):
    Print ("search pytest")

def test_case2_02(open_baidu):
    Print ("search blog Park")

test_ Cnblogs directory

No,__init__.pyThe file also has no confitest Py file

def test_no_fixture(login):
    Print ("without _init _ file, enter cnblogs directly", login)

test_ Taobao directory

Configure the unique fixture for the test case of Taobao website

import pytest

def open_taobao(login):
    name = login
    Print (F "user {name} enters Taobao")

class TestTaobao:
    def test_case1_01(self, open_taobao):
        Print ("purchase goods")

    def test_case1_02(self, open_taobao):
        Print ("enter the settlement interface")

Run py

Here’s an article about confitest in pytest This is the end of the article on the usage of py. More about pytest confitest Py content, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you can support developeppaer in the future!

Recommended Today

ffmpeg pushes video with RTP protocol

docker build environment docker pull abdulachik/ffmpeg.js:latest docker run -it -p 8080:8080 -v /Users/workspace/Downloads/ffmpeg_wasm:/tmp –privileged=true abdulachik/ffmpeg.js:latest /bin/bash cd /tmp Process overview camera->ffmpeg->encode to h264 video->pass to h264 decoder->play(You can also push the video directly to RTMP protocol after encoding, and the local decoding and playback is only for testing) ffmpeg encodes video stream (from camera) to […]