Pytest is the built-in automation testing framework for Python 2 , while for Python 3 the pytest framework is independent and needs to be installed via pip installation
1. Installing and Setting Up Pytest
1. Use pip install -U pytest to install for Python 3
2. Check pytest version information: pytest –version
3. Execution rules for pytest cases:
① Test files must be named test_xx.py and should start with test_ (or end with _test)
② Test classes must start with Test_ and should not contain an __init__ method
③ Test functions or methods must start with test_xx, generally divided into three stages:
1. Writing test cases
2. Executing test cases
3. Generating test reports
Step 1: Writing Test Cases
Pytest executes by default the test classes or methods in files that start with test_ or end with _test in the specified path, and it defaults to executing classes that start with Test_ and methods that start with test_.
1. First, create a .py file named to start with test_ or end with _test
2. If creating a new class, the test class needs to start with Test_
3. Test cases (methods) need to start with test_
1.1 Optimizing Test Cases
1.1.1 assert Assertion (to check if the execution result meets expectations):
The five most commonly used assertions in Pytest are as follows:
assert xx: checks if xx is true assert not xx: checks if xx is not true assert a in b: checks if b contains a assert a == b: checks if a equals b assert a != b: checks if a does not equal b
Add assertions in the corresponding methods to check if the test case results meet expectations, as shown below:
The execution result shows: the result of the assertion failure will be displayed.
1.1.2 Assertion Optimization
If you want to see more detailed information when a failure occurs, you can add a description to the assertion, as shown below:
The execution result is shown in the following figure, where you can see the description information of the assertion when it fails.
Step 2: Executing Test Cases
2.1 Using Command Line for Execution
Open the cmd window, then execute pytest file_path/test_file_name, for example pytest ./test_tt.py
If the current path is already the folder where the test case files are located, you can directly input pytest test_file_name:
If you only input pytest, it will default to execute all files in the current folder that start with test_ (or end with _test).
2.2 Executing in IDE (PyCharm)
The syntax is as shown in the screenshot, pytest.main() (parameters given in the form of a list, as shown in the screenshot)
Execute the file located in the current path, specifying the file name will execute the specified file, if not specified, it will execute all files starting with test_ or ending with _test.
2.3 Executing Specific File and Method
2.3.1 Command Line Syntax
pytest path/file_name::class_name::method_name, for example:
2.3.2 PyCharm Syntax
If using PyCharm, the syntax is as shown in the image, equivalent to the command line:
2.4 Executing with Parameters
Common pytest executions with parameters: several types are listed, others can be searched on Baidu.
2.4.1 pytest -q Simplifies Console Output
Execution result without parameters:
Execution result with parameters:
2.4.2 pytest -v Outputs More Detailed Case Execution Information, Such as Case File and Case Name
Execution result before adding parameters:
Execution result after adding parameters:
2.4.3 pytest -k Executes Cases Containing ‘Keyword’
Cases to be executed are shown in the screenshot:
Execution code and result as shown: (the keyword needs to be in double quotes)
2.4.4 pytest -s Outputs Debug Information in the Case, Such as Print Output, If No Parameters Are Added, It Will Not Output
Case to be executed:
Execution result before adding parameters:
Execution result after adding parameters:
2.4.5 pytest -m Executes ‘Marked’ Content, Executes Specific Test Cases, Executes Test Cases with the Same Mark, The Method to Add a Mark is as Follows:
Test cases to execute:
Execution code and result as shown:
Executing test cases with the same mark can be used to specify which test cases to execute.
2.4.6 pytest -x Stops Execution on Failure, Subsequent Cases Will Not Be Executed
Cases to be executed:
Execution result with parameters:
2.4.7 pytest –maxfail=n Stops Execution After n Failures, Where n Is the Number of Failures
Assuming n = 2, executing the above cases: since there was only one failure, the case will continue to execute until two failures stop execution.
2.4.8 pytest –count=n Executes Test Case n Times, n=2 Means Execute Twice
For example, execute test_01 twice:
2.4.9 pytest –lf (last failed) Re-runs the Last Failed Test Cases, If There Are No Failures, All Will Be Run
2.4.10 pytest –ff (failed first) Re-runs All Test Cases, But First Runs the Last Failed Test Cases
2.5 Introduction to Quality Features
2.5.1 Skipping Case Execution
Based on specific conditions, do not execute the marked test function. Method:
skipif(condition, reason=None) Parameters:
condition: the condition to skip, required reason: the reason for skipping, required
Usage:
@pytest.mark.skipif(condition, reason=”xxx”) condition is true when skipped
@pytest.mark.skip()
Example:
2.5.2 Marking a Test Case as Expected Failure
Marking a test function as a failure function
Method:
xfail(condition=None, reason=None, raises=None, run=True, strict=False)
Common Parameters:
condition: the condition for expected failure, required reason: the reason for failure, required
Usage:
@pytest.mark.xfail(condition, reason=”xx”) condition is true then marked as failure
When a certain condition is not met, it is expected to fail, and thus marked as expected failure. If
the condition is not met, it executes normally
Example:
No parameters:
When the condition is met:
When the condition is not met:
2.5.3 Parameterization
Method:
parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
Common Parameters:
argnames: parameter name
argvalues: corresponding values, must be of list type when there is one parameter: [value]
When the number of parameters is greater than one, the format is: [(param_value1,param_value2…..),(param_value1,param_value2)]
Usage:
@pytest.mark.parametrize(argnames,argvalues) parameter name, parameter value @pytest.mark.parametrize(“a”,[3,6]) single parameter @pytest.mark.parametrize(“a,b”,[(1,2),(0,3)]) multiple parameters
The parameter value is N, the test method will run N times
Example:
Single parameter:
Multiple parameters:
2.5.4 Execute Marked Cases Multiple Times
First install repeat:
pip install pytest-repeat
@pytest.mark.repeat(n) executes the current case n times and then proceeds to execute other cases
Example:
2.5.5 Adjusting the Execution Order of Cases
Use:
Install pip install pytest-ordering and add the decorator @pytest.mark.last for the last execution
@pytest.mark.run(order=1)—the order of execution
Example:
Last execution:
Specify execution order:
2.5.6 Custom Test Case Precondition — The Essence of Pytest Fixture
@pytest.fixture()(scope=”function”,params=None,autouse=False, ids=None, name=None)
Called first for preprocessing or repetitive operations. Scope: the scope of the marked method
function (default): applies to each test method, each test runs once
class: applies to the entire class, all tests in each class run only once
module: applies to the entire module, all tests in each module run only once
session: applies to the entire session (use with caution), runs only once for each session
params: (list type) provides parameter data for the function using the marked method
autouse: whether to run automatically, defaults to False (not running), set to True to run automatically
If not True, it needs to be called to execute first.
2.5.6.1 Fixture Usage One
Defining a fixture is similar to defining a normal function, naming should not start with test to distinguish from cases, fixtures have return values, and if there are no return values, the default is None. The case calls the fixture return value, directly using the fixture function name as the variable name.
Example:
Will be executed prior to test case test_01
2.5.6.2 Fixture Usage Two
Functions marked with fixture can be applied outside the test class, and will always execute first.
2.5.6.3 Fixture Usage Three
Passing parameters as identifiers
2.5.6.4 Using Multiple Fixtures
If the case needs to use multiple fixture return data, the fixture can also return a tuple, list, or dictionary, and then extract the corresponding data from it.
Multiple fixtures can also be passed values in the case from multiple fixture parameters.
2.5.6.5 Fixture Scope
2.5.6.5.1, function executes once before each method
2.5.6.5.2, class executes once at the beginning
2.5.6.5.3, module executes only once before all cases in the current .py script
2.5.6.5.4, session level can be called across .py modules
That is, when we have multiple .py files of cases, if multiple cases only need to call the fixture once, it can be set to scope=”session”.
2.5.6.5.5, conftest.py file
Create a conftest.py file, the name is fixed, and it is in the same package as the running case, no need to import pytest will automatically search, the file can independently manage some preset operation scenarios, which will be executed during the test case execution. Example:
Step 3: Generating Test Reports
To generate a test report, you need to first install pytest-html with the installation command
pip install pytest-html
3.1 Generating Reports Using Command Line
–html=path/file_name.html If no path is specified, it defaults to the current path
3.2 Generating Reports Using PyCharm
The generated report looks something like this:
3.3 High-End Test Reports
If you want to generate high-end test reports, search for Pytest+Allure, which can be integrated into Jenkins to automatically generate high-end automated test reports. The effect image is roughly like this:
3.4 Generating XML Reports
–junit-xml=path/file_name.xml
The effect is roughly like this:
Extensions
Output Coverage Report
pytest can output coverage HTML reports using the following command:
pytest -vv –cov=./ –cov-report=html You may encounter an error:
pytest: error: unrecognized arguments: –cov-report=html
? inifile: None
? rootdir: /Users/joe/workspace/platform/mgap-mendel/mgap-mendel The reason:
Missing the pytest-cov package
Solution
pip install pytest-cov It looks like this:
Setting the Scope for Repeated Execution
–repeat-scope
–repeat-scope is similar to the scope parameter of pytest fixture, –repeat-scope can also set parameters: session, module, class, or function
function scope repeats execution for each case before moving to the next case
class repeats execution for all cases within the class, then moves to the next module, repeating execution for all cases within the module, then moves to the next
session repeats the entire test session, that is, all collected tests execute once, then all these tests execute again, and so on
Example: as below scope=function –count=2 executes each case twice
Executing each case twice, if it is a class, it executes the cases within the class repeatedly
Repeat Testing Until Failure
Combine pytest’s -x option with pytest-repeat to force the test runner to stop on the first failure. For example:
pytest –count=1000 -x test_tt.py
This will execute the case 1000 times, stopping on the first failure. Multiple parameters can be added during execution; more details need to be explored.
Configuration Files
Configuration files can change the running rules of the pytest framework, such as changing the rules for collecting pytest cases, adding command parameters.
The configuration as follows addopts = -s –html=./report.html will automatically generate reports when executing pytest
python_files = test_*.py python_classes = Test_* python_functions = test_*
The content can change the rules for collecting pytest cases, for example, modifying python_files = test_*.py check_*.py will also automatically execute files starting with check_ when executing pytest.
To use the configuration file, create a pytest.ini file in the project root directory and configure the parameters as follows:
[pytest]
# Command line parameters addopts = -s # Search file name
python_files = test_*.py # Search class name python_classes = Test_* # Search function name
python_functions = test_*
Setup and Teardown Functions
Pytest is an extension of unittest, and can also implement setup (executed once at the beginning) and teardown (executed once at the end). This is mentioned briefly; for detailed content, you can search on Baidu.
Pytest supports setup/teardown but recommends using pytest.fixture instead.
Fans Exclusive
Organized resources worth over 2000+
100G resources
Content includes:
From 0-1 planning software testing learning path
Commonly used testing templates, strategies in the workplace
Software testing improvement e-books
Classic interview questions
Songqin recorded courses
Limited time free~~~
Long press the image below
Follow Teacher Tang from Songqin to get 100G resources for free
Currently, over 100,000 people have followed and joined us
Long press the QR code
Follow the 【Songqin Online Course】 video number