<> one , What is a unit test framework

Unit test framework refers to : In software development , Minimum unit for software ( function , method ) Check and test the correctness .

<> two , Classification of unit test framework

Java: Juint,TestNG
Python: unnitest,pytest

<> three , What is the unit test framework used for ?

* Test findings : Find test cases from multiple files
* Test execution : Execute according to certain order and rules , And generate results
* Test judgment : Judge the difference between expected results and actual results through assertions
* Test report : Statistical test progress , time consuming , Pass rate of use case , Generate test report
<> four , What is the difference between a unit testing framework and an automated testing framework ?

(1) What is an automated testing framework ?

To complete automated testing of a specified system , A complete package , Perfect code framework .

(2) Role of automated test framework

*
Improve test efficiency , Reduce maintenance costs ‘

*
Reduce manual intervention , Improve test accuracy , Increase code reusability

*
The core idea is : So that people who do not understand the code can also realize automatic testing through the framework .
(3)pytest Relationship between unit test framework and automated test framework

*
pytest The unit test framework is only one part of the automated test framework .

*
pom: It's just part of an automated testing framework

*
Data driven :

*
Keyword Driven :

*
Encapsulation of global configuration files

*
Log monitoring :

*
seleinumr,equests Secondary packaging

*
Assert

*
Test report

*
mail

<> five ,pytest Framework introduction

1,pytest Is a very mature python Unit test framework , than unittest More flexible , Easier to use .
2,pytest Can and seleium,appinum,requests realization Web automation ,App automation , And interface automation .
3,pytest Test cases can be skipped , and reruns Failed test case retry
4,pytest Can combine ,allure Achieve a very beautiful test report
5,pytest Can and Jenkins Achieve continuous integration .
6,pytest There are many powerful plug-ins , And these plug-ins can realize many practical operations .
pytest
pytest-html ( generate html Format report )
pytest-xdist ( Distributed execution of test cases , many CPU distribute )
pytest-ordering ( Used to change the execution order of test cases )
pytest-rerunfailures ( Retry for failed cases )
allure-pytest( Used to generate beautiful test reports )

Batch download plug-ins
put to requirements.txt In the file , implement pip install -r requirements.txt This command

<> six , use pytest The rules and basis of default test cases are used

1,pytest Usage rules of

* Module name must be test_ Beginning or _test ending
* Test class must be Test start , And there can be no init method
* The test method must be test start
2, By reading pytest.ini Profile run

<> seven ,pytest Operation mode of test case

1, Main function mode
(1) Run all :pytest.main()
(2) Designated module :pytest.main(["-vs", “test_login.py”])
(3) Specify directory : pytest.main(["-vs", “./interface_testCase”])
(4) adopt nodeid Specify use case run ,nodeid By module name , Separator , Class name and method name
pytest.main(["-vs", “./interface_testCase/test_interface.py::test_04_func”])
pytest.main(["-vs",
“./interface_testCase/test_interface.py::TestInterface::test_03_shaheshang”])

test_interface.py
def test_04_func(): print(" Test function ") class TestInterface: def test_03_shaheshang(
self): print(" Test sand monk ")
all.py
import pytest if __name__ == '__main__': pytest.main(["-vs",
"./interface_testCase/test_interface.py::test_04_func"]) Operation results :
interface_testCase/test_interface.py::test_04_func Test function PASSED
2, Command line mode
(1) Run all :pytest
(2) Execution module :cd web_testCase Switch to the directory where the specified module is located
pytest -vs test_login.py
(3) Specify directory : pytest -vs ./interface_testCase
(4) adopt nodeid Specify use case run : pytest -vs
./interface_testCase/test_interface.py::test_04_func

3, By reading pytest.ini Profile run
pytest.ini This file is pytest Core configuration file of unit test framework .

* position : It is usually placed in the root directory of the project .
* code : Must be ANSI
* effect : change pytest Default behavior rules
* Running rules : Regardless of the mode of the main function , Or run in command line mode , Will read this configuration file . [pytest] addopts = -vs #
Command line parameters , Separated by spaces # Test case path testpaths = './web_testCase' # Module name rule python_files =
'test*.py' # Rules for class names python_classes = Test* # Rules for method names python_functions = test
4, Detailed explanation of parameters
Detailed explanation of parameters : -s: Indicates output debugging information , include print printing of messages -v: Show more detailed information , Including the module name and class name where the use case is located -vs: These two parameters can be used together
--reruns2: Failed case rerun -x : Indicates that as long as there is an error in one use case , Then the test stops --maxfail=2: Indicates that two use cases stop when they fail -k:
Specify the test case according to the partial string of the test case def test_01_sunwukong def test_02_xiaohong eg: pytest -vs
./web_testCase -k'on' --html ./report/report.html generate html Test report in format -m : Execute the use case of the specified tag
eg: -m ’smoke‘ Flag execution only smoke Use case -n: Support multi-threaded or distributed test cases - Main function mode pytest.main(["-vs",
"./web_testCase/test_login", "-n=2"]) - Command line mode writing pytest -vs
./web_testCase/test_login -n2 ` If so 5 Use cases , If two threads are allocated for execution , Then the first thread will execute 1 3 5 Use case , The second thread executes 2
4 Use case
<> eight , Failed case retry

The parameters used for retry of failed case are :–reruns==2
test_login Zhongyou 5 Use cases , The simulation failed the execution of the second use case
class TestLogin: def test_01_sunwukong(self): print(" Test Monkey King ") def
test_02_xiaohong(self): print(" Test Xiaohong ") assert 1 == 2 def test_03_huahua(self):
print(" Test flower ") def test_01_xiaoming(self): print(" Test Xiaoming ") def test_01_dudu(self):
print(" Test beep ")
all.py
import pytest if __name__ == '__main__': pytest.main(["-vs", "./web_testCase",
'--reruns=2'])
Execute using command line mode :
pytest -vs ./web_testCase --reruns 2
<> nine ,pytest The order in which test cases are executed

unittest: adopt ASCII To determine the order of execution
pytest: Default from top to bottom

install :pytest-ordering This plug-in
The role of this plug-in : Used to change the execution order of test cases
Can pass mark Tag to specify the order in which the use case is executed
@pytest.mark.run(order=1) def test_07_xiaohong(self): print(" Test Xiaohong ")
<> ten , How to execute in groups

1, Group execution usage scenario
smoke , Sub module execution , Sub interface and web implement
smoke: Smoking case , Distributed in each module
stay pytest.ini Add to configuration file
markers = smoke: Smoking case usermanage: User management module productmanage: Commodity management module
Add tags to use cases
@pytest.mark.smoke @pytest.mark.usermanager def test_07_xiaohong(self): print(
" Test Xiaohong ")
pytest -vs -m ‘smoke’
pytest -vs -m ‘somke or usermanager’

Or in pytest.ini In the file , Configure the use case to run the specified tag
[pytest] # Command line parameters , Separated by spaces addopts = -vs -m 'smoke' # test case folder , Self configurable testpaths =
'./web_testCase' # Module file name of configuration test search python_files = 'test*.py' # Configure the test class name of the test search
python_classes= Test* # Test function name for configuring test search python_functions = test markers = smoke:
Smoking case usermanage: User management module productmanage: Commodity management module
stay test_login in , Use case tag
@pytest.mark.smoke def test_07_xiaohong(self): print(" Test Xiaohong ")
stay all.py Execute in file
import pytest if __name__ == '__main__': pytest.main(["-vs", "./web_testCase"])
<> eleven ,pytest Skip test case

Usage scenario : Execute only forward use cases , Do not execute reverse use cases , Exception use case

Add on use case skip sign
Unconditional skip :
@pytest.mark.skip(reason=“ Do not execute flowery use cases ”)
Conditional skip :
@pytest.mark.skipif(age >= 18, “ Under age ”)

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform