当前位置:网站首页>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 == 31、 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 == 3Then 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
label2pytest 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_*
边栏推荐
- 二叉树的基本性质与遍历
- Learn together and make progress together. Welcome to exchange
- Splicing audio files with ffmpeg-4.3
- After a few years in the testing industry, do you still know a little?
- Responsibility chain mode -- through interview
- Basic concepts and definitions of Graphs
- Sequential stack traversal binary tree
- Visitor model -- generation gap between young and middle-aged people
- Analysis of errors in JSON conversion using objectmapper
- 使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value
猜你喜欢

Grating diffraction

Background of master data construction

When querying the database with Gorm, reflect: reflect flag. mustBeAssignable using unaddressable value

物联网?快来看 Arduino 上云啦

Basic properties and ergodicity of binary tree

Enjoy yuan mode -- a large number of flying dragons

After a few years in the testing industry, do you still know a little?

Apple, Microsoft and Google will no longer fight each other. They will work together to do a big thing this year

The Google File System (GFS) learning notes

Learn to use a new technology quickly
随机推荐
The Google File System (GFS) learning notes
[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock
Vant component used in wechat applet
Enjoy yuan mode -- a large number of flying dragons
传统的IO存在什么问题?为什么引入零拷贝的?
Selenium crawl notes
More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
DX12引擎开发课程进度-这个课程到底讲到哪里了
Create a multithreaded thread class
Mapstacks: data normalization and layered color layer loading
Variable setting in postman
Leetcode (455) - distribute cookies
Dongyuhui is not enough to bring goods to "rescue" live broadcast
Where is 5g really powerful? What is the difference with 4G?
Interpreter mode -- formulas for dating
2021-09-30
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
顺序表的基本操作
Reflection - class object function - get method (case)
Dx12 engine development course progress - where does this course go