当前位置:网站首页>Pytest testing framework

Pytest testing framework

2022-06-24 21:05:00 gchh_ spring

pytest Introduce :

pytest Is a very mature full-featured python The test framework , And python Self contained unittest The test framework is similar to , but pytest It is more concise and efficient to use , And compatible unittest frame

pytest Support simple unit testing and complex functional testing , combination requests Implement interface testing , combination selenium、appnium Realize automatic function test

pytest combination allure Automatic generation of test report can be realized

pytest Support a variety of third-party plug-ins , Can follow Jenkins Integrate

pytest Visit website :https://docs.pytest.org/en/stable/

pytest install ( A brief introduction ):

adopt python Package management tools pip Installation :pip install pytest; upgrade pytest:pip install -U pytest

pytest Test case identification and operation :

Use case name specification :

1、python Document to test_ start , Or with _test ending :test_*.py、*_test.py

2、 Test class to Test start , And not with init Method

3、 Test the use case method in a function or test class with test_ start

function pytest The test case :

1、 Can be directly in terminal Direct execution pytest or py.test, At this time, it will automatically find all that meet the specifications under the current path py Document and execute test cases that conform to the specifications

2、pytest -v   Print detailed running log information , For example, the file path of each use case and which class it belongs to

3、pytest -s  Output results with console , When there is print Output statement , If you want to print in the running results print Information about , You can add -s Parameters

4、pytest -k  Run a type of use case with a keyword , How to use it is as follows :

    pytest -k 'classname'  Run all the methods in the class , The match here is not an exact match , It's fuzzy matching

    pytest -k 'methodname'  Run a method , Empathy , Fuzzy matching

    pytest -k 'name1 or name2'  Run all matching use cases ,or Is a juxtaposition relationship , Fuzzy matching

    pytest -k 'classname and not methonname'  Run all the methods in the class , Does not contain a method

5、pytest -x  The case stops running immediately in case of failure

6、pytest --maxfail=[num]  Stop running when the number of case failures reaches the set value

7、pytest -m [ Tagnames ] Will run with @pytest.mark.[ Tagnames ] This marked test case , In the process of automation, test cases can be labeled and classified

8、--lf,--last-failed  Run the last case that failed to execute , The use cases that did not fail last time are all executed

9、--ff,--failed-first  Execute the last failed use case first , Execute the last successful use case

10、pytest --collect-only Collect only use cases , Do not execute the use case

Operation mode :

1、 Use pycharm The built-in debugging method , There is a green button in front of the use case method name , Click directly to run

To use this method, you need to be in pycharm Set the operation mode as pytest,File-Settings-Tools-Python Integrated Tools

2、 Use pytest function

    pytest file name .py  Execute a... Alone py file

    pytest file name .py:: Class name   Execute a... Alone py Class in file

    pytest file name .py:: Class name :: Method name   Execute a... Alone py A method of a class under the file

3、 Use python The interpreter runs

pytest Parameterized use cases

Application scenarios : Test data is passed in , The expected results of the test are also passed in , Two different parameters correspond to each other

1、 adopt @pytest.mark.parametrize(argnames, argvalues) To parameterize

argnames: Variables to parameterize ,string( Separate with commas ),list,tuple

argvalues:argnames Each corresponding value to be parameterized ,list,list[tuple], Need and argnames One-to-one correspondence

@pytest.mark.parametrize(['a', 'b'], [(1, 2), (2, 3)])
def test_param(a, b):
    c = a + b
    print(f"{a} + {b} eq {c}")

The parameterized code in the figure above can also be modified in the following ways :

@pytest.mark.parametrize(('a', 'b'), [(1, 2), (2, 3)])
@pytest.mark.parametrize("a, b", [(1, 2), (2, 3)])

above test_param The code runs as follows : You can see the code of this use case , The operation results are 2 Test cases [(1-2),(2-3)], Because the input is parameterized , And the name of the test case is automatically generated , Format : Method name [ Parameter values - Parameter values ]

Can be in parametrize Parameter ids Parameters , take pytest The auto named name is replaced with ids The name defined in

@pytest.mark.parametrize("a, b", [(1, 2), (2, 3)], ids=["case1", "case2"])
def test_param(a, b):
    c = a + b
    print(f"{a} + {b} eq {c}")

2、 Stacking of parameters

#  Stacking of parameters 
@pytest.mark.parametrize("a", [0, 1])
@pytest.mark.parametrize("b", [2, 3])
def test_param2(a, b):
    c = a + b
    print(f"{a} + {b} eq {c}")

The output of the run : Stack according to parameters , Different combinations of test data can be generated

3、 adopt yml Data files to parameterize use cases ( Data driven , You can also use json、excel And other data files )

Can be 1 The parameterized data of the chapter is written into a yml In file , By reading the yml Parameterize the preset data in

yml Basic tutorial reference :https://www.runoob.com/w3cnote/yaml-intro.html

step 1: First, create a new one data.yml file , And write the following data in the file :

step 2: Read... Is used in parameterization yml The data is transferred into the file content :

#  adopt yml File read test data , You need to start importing from the file yaml modular 
@pytest.mark.parametrize("a, b", yaml.safe_load(open("./data.yml"))['datas'],
                         ids=yaml.safe_load(open("./data.yml"))['ids'])
def test_param3(a, b):
    c = a + b
    print(f"{a} + {b} eq {c}")

step 3: Running results :

The above contents can be further optimized or encapsulated :

1、 take yaml.safe_load(open("./data.yml"))['datas'] Call directly after function encapsulation

#  Definition acquisition yml File data functions 
def get_datas():
    with open("./data.yml") as f:
        yml_data = yaml.safe_load(f)
        print(yml_data)
        datas = yml_data['datas']
        ids = yml_data['ids']
        return [datas, ids]

@pytest.mark.parametrize("a, b", get_datas()[0], ids=get_datas()[1])
def test_param4(a, b):
    c = a + b
    print(f"{a} + {b} eq {c}")

 

pytest -m Parameters,

pytest -m [ Tagnames ] Will run with @pytest.mark.[ Tagnames ] This marked test case , In the process of automation, test cases can be labeled and classified , For example, classify the levels of test cases , Basic function use cases , Smoking cases, etc

such as , The following code snippet , The two test cases are marked with label1 and label2

class TestDemo:
    @pytest.mark.label1
    def test_haha(self):
        assert 1 == 1

    @pytest.mark.label2
    def test_hehe(self):
        assert 2 == 3

1、 have access to pytest -m "label1" test_mark.py Only run the tag label1 Test cases for , The running result is 1 Use cases :test_haha

2、 have access to pytest -m "label1 or lable2" test_mark.py Run marked label1 perhaps label2 Test cases for , The running result is 2 Use cases

3、 take test_hehe Change the use case of the to the following tag , A use case can tag multiple tags :

@pytest.mark.label1
@pytest.mark.label2
    def test_hehe(self):
        assert 2 == 3

Then you can use pytest -m "label1 and lable2" test_mark.py The run also marks label1 and label2 Test cases for , The running result is 1 Use cases :test_hehe

4、 When a use case is executed with a tagName that you define , The running result will appear warning, have access to pytest.ini File configuration after custom label elimination warning Information

Create a pytest.ini file , And make the following settings :

[pytest]
markers = label1
          label2

pytest Frame structure

pytest Provides setup and teardown To implement the pre - and post conditions of use cases , For example, a certain type of use case needs to be logged in before the steps are executed , Log out after execution , Login and logout operations can be overwritten using the same method

According to the operation level of use cases, they can be divided into the following categories :

Module level :setup_module,teardown_module, Call at the beginning of the module setup_module Method , End call teardown_module Method , stay python in , One py File is a module

Function level :setup_function,tearndown_function, Call the two methods at the beginning and end of the function ( Functions not in the class ,setup Start ,teardown end )

Class level :setup_class,tearndown_class, Call two methods at the beginning and end of the class

Method level :setup_method,teardown_method, Call... At the beginning and end of the method, respectively ( Used exclusively for methods in classes )

Method level :setup,teardown, If defined in a class , Scope of use and setup_method、teardown_method equally ; If you define... Outside a class , Scope of use and setup_function、teardown_function equally

The order of the calls :

setup_module>setup_class>setup_method>setup>teardown>teardown_method>tearndown_class>teardown_module

Customize pytest The operating rules of :

1、 Custom tagnames , With the above pytest -m The operating parameters are used in conjunction with , It can be eliminated after customization warning

2、 Customize the default parameters added by the runtime

      addopts = -vs

3、 Customization can be pytest Identified running files 、 Operation class 、 Operation case, etc

[pytest]
markers = label1
          label2
addopts = -vs
python_files = check* test*
python_classes = Test*
python_funtions = check_* test_*

 

 

 

 

 

 

 

 

 

 

原网站

版权声明
本文为[gchh_ spring]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211321069617.html