pytest environment deployment
After the python environment is successfully installed
pip install pytest
- By default, pytest reads all folders and files starting with test.
- fixture is a powerful tool in pytest.
- Assertion mechanism: assert.
- PIP pytest html installation test report
- By default, pytest looks for folders, files and functions starting with test in all files and subfolders under the current path as identification objects.
- pytest does not output any print information by default. If you want to have print information, you need to add the - s instruction at run time. Multiple instructions are divided in the main function in the same run time.
-s print information
-v is used to display log information in detail
-Simple statistics of rA test results
-m classification execution
if __name__=='__main__': pytest.main(['-s','-v'])
Preset function (used for early data preparation)
- Generally, it can be managed through a configuration file: the name of the configuration file must be confitest Py, it can't be anything else.
- The scope parameter defines four levels
Session: execute only once in this session level.
Module: executed only once at the module level.
Class: executed only once at the class level.
Function: executed once at the function level.
The default level is function
conftest. Py (file name of the configuration file)
@pytest.fixture(scope='calss') def A(): Test function parameters A Test function def test_case(A)
Pre and post conditions
Pre and post conditions outside the class
Preconditions and postconditions at the function level def setup_function(): print('Preconditions') def teardown_function(): print('Post condition')
Preconditions and postconditions at the module level def setup_module(): print('Module preconditions') def teardown_module(): print('Module post condition')
Pre and post conditions within a class
Definition of calss object in pytest: it is recommended to start with test
class TestDemo: def test_d1(self): print('Execute function 1 of class') def test_d2(self): print('Execute function 2 of class') def setup(self): print('Preconditions for in class functions') def teardown(self): print('Post condition of function in class') def setup_class(self): print('Within class class Level preconditions') def teardown_class(self): print('Within class class Level post condition') def setup_method(self): print('Within class method Level preconditions') def teardown_method(self): print('Within class method Level post condition')
Run order level of pre post functions in class:
Test case management method Mark
All use cases can be marked through the mark decorator, and different marks can be managed separately
@pytest.mark.Tag name (English) Test case code Execute command pytest -s test_case.py -m Tag name Run test cases with multiple tags pytest -m "p1 or p2" Run yes p1 Label or p2 Labeled use cases pytest -m "p1 and p2" Run yes p1 Label and have p2 Labeled use cases pytest -m "not p2" Operation has except p2 Use cases other than Tags pytest -m "p1 and not p2" Run yes p1 Label and no p2 Labeled use cases
Note that the - m command cannot be followed by a '' (single quotation mark), but only by a "" (double quotation mark), otherwise it cannot be recognized.
Core configuration file under pytest framework
The configuration can take effect globally under the root path of the project. The file name is pytest Ini, you can define the running rules of various pytests.
Use @ pytest. Only mark. There will be a warning prompt in the tag name. Adding the configuration file can eliminate the warning prompt.
[pytest] #Declaration definition tag name markers = webui : automation for webui interface : automation for information temp : just for fun #Defines the file name specification of the read recognition object, and multiple file names are separated by spaces python_files = cema*.py test*.py #Defines the naming convention for the read recognition class (class object) python_classes = cema* #Make a specific path to run testpaths = test_demo #Defines the specified name of the read function python_functions = vip* #Default instruction added at run time addopts = -s -v --html =./report/report.html --self-contained-html
Assertion mechanism: assert
assert conditional statement, 'error information'
#html report command --html =./report/report.html --self-contained-html #There is a space after html, and there are two places in the above command #Terminal command, followed by -- self * * * a series of commands to combine the report content and html format into a file. If not, there will be an html format file pytest --html =./report/report.html --self-contained-html