Python automated testing framework learning diary

pytest is a very mature and full-featured Python testing framework with the following characteristics:

Simple and flexible, easy to use

Support parameterization

It can support simple unit tests and complex functional tests, and can also be used to do selenium/appnium and other automated tests, interface automated tests (pytest+requests)

Pytest has many third-party plug-ins, and can be customized extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (repeated execution of failed cases), pytest-xdist (Multi-CPU distribution) etc.

Skip and xfail processing of test cases

Can be well integrated with jenkins

report framework----allure also supports pytest

Install Pytest

pip install -U pytest

Pytest use case design principles

The test class starts with Test and cannot have an init method

Functions starting with test_

Classes beginning with Test

All packages pakege must have __init__.py file

Assert using assert

Two ways to run Pytest

Code running in Pycharm

pytest.main([“test.py”])

Code running in Pycharm

pytest test.py

Run the specified method under the specified class

pytest file name::class name::method name

Pytest parameter description

-v Description: You can output more detailed execution information of the use case, such as the file where the use case is located and the name of the use case, etc.

-s Description: Enter the tuning information in our use case, such as print information, etc.

-x: Use case with error, exit execution immediately, and output the result

-v: means to view detailed report content

-collect-only: Indicates that all the use cases to be executed are displayed

-lf: Only execute the use case that failed last time

-vv: Display detailed test results

-tb=no: Do ​​not display the error details of the use case failure

-tb=line: show the specific number of lines of code where the use case failed

-tb=short: display more detailed error information

-k "Keywords" Description: execute use cases that contain "keywords"

-q Note: Simplify the console output, you can see that the output information is different from the above result. There are two dots in the figure below instead of the pass result

-maxfail=num When the use case error reaches the specified number, stop the test

m Description: Execute specific test cases. Let's modify our use case again and add a new use case

If you want to run multiple flags, use expressions, as follows

pytest -m "slow or faster" test_1.py runs a use case with a slow logo or a faster logo

pytest -m "slow and faster" test_1.py runs the use cases identified by slow and faster

pytest -m "slow and not faster" test_1.py run use cases with slow and no faster logos

Note: "" (single quotation mark) cannot be followed by -m, only "" (double quotation mark) can be used, otherwise it will not be recognized

ini configuration file

Create pytest.ini file (fixed writing)

[pytest];Fixed writing

; Variable name cannot be wrong

addopts=-vv -s; spaces between multiple parameters

testpaths=…/HC/huace; spaces between multiple directories

python_files=test*.py; python file prefix, customizable

python_classes=huace; specify the class name

python_functions=test*; Specify the method name, which can be customized

Skip test function

Skip test function: According to specific conditions, the identified test function is not executed

-- coding: utf-8 --

import pytest

class Test():

def test(self):

    print("执行的是testcase的用例")

@pytest.mark.skipif(condition=1<2, reason="1 is not greater than 2, so it is not executed")

class huace():

def haha(self):

    print("执行的是haha方法里面的用例")
  •  

Guess you like

Origin blog.csdn.net/woaisjm/article/details/112816325