Command line parameters of Pytest framework 2

 

Preface

The previous blog mentioned how to use some parameters of command line execution test cases? Today we will continue to update the use of some other command options and the rules for pytest to collect test cases!

Pytest execution case command line parameters

--collect-only: List all test modules, test classes and test functions in the current directory

--tb=style: Shields the traceback information output by test case execution, which can simplify the output information when the test case fails. Style can be on, line, or short. Please experience the specific differences by yourself.

 

 --lf: When a test case is executed, if there are failed test cases, we can use this command to rerun the failed test cases.

When we execute the use case for the first time, we will see that 2 use cases failed.

 We use the --lf parameter to run the use case again, and we can find that only the two previously failed use cases were re-executed.

 --ff: If the last test case failed, when --ff is used, the failed test case will be executed first, and the remaining test cases will be executed again.

summary

The above are the parameters that are often used when running test cases on the command line. These parameters can be used not only individually, but also in combination. Later, they will also involve some commands when using fixtures, which you don’t need to understand now. You can use --help to view some command help information!

Pytest rules for collecting test cases

1) Start searching from one or more directories. You can specify files or directories on the command line. If not specified, start collecting use cases from the current directory.

2) Recursively search for test modules in this directory and all subdirectories

3) The test module refers to the file named test_*.py or *_test.py

4) Find functions starting with test_ in the test module

5) Find the class whose name starts with Test. First, filter out the classes containing the __init__() function, and then find the class methods starting with Test_ in the class.

Rule verification

Now we will demonstrate the process of pytest searching for test cases in sequence.

First we create a new project according to the following directory structure

 

Write the following code for each file (we are just to verify the rules, so the use cases are very simple, there will be no such simple use cases in actual projects)

test_test module1.py

1 # Test function
 2 
 3 def test_2():
 4     assert 1 == 1
 5 
 6 
 7 # Ordinary functions
 8 def func_2():
 9 print('Ordinary function')
10 
11 # Test class
12 class TestClass_2(object):
13 
14 # Test function
15     def test_class_3(self):
16         assert 1 == 1
17 # Ordinary functions
18     def func_class_3(self):
19         assert 1 == 1
20 
21 # Ordinary class
22 class NoTestClass_2(object):
23 # Test function
24     def test_class_4(self):
25         assert 1 == 1
26 
27 # Ordinary functions
28     def func_class_4(self):
29         assert 1 == 1

test_test module 2.py

1 # Test function
 2 
 3 def test_1():
 4 
 5     assert 1==1
 6 # Ordinary functions
 7 def func_1():
 8 print('Ordinary function')
 9 
10 # Test class
11 class TestClass_1(object):
12 # Test function
13     def test_class_1(self):
14         assert 1==1
15 
16 # Ordinary function
17     def func_class_1(self):
18         assert 1==1
19 # Ordinary class
20 class NoTestClass_1(object):
21 
22 # Test function
23     def test_class_2(self):
24         assert 1 == 1
25 
26 # Ordinary functions
27     def func_class_2(self):
28         assert 1 == 1

testcase.py

1 # Test function
2 def test_one():
3     assert 1==1
4 
5 # Ordinary function
6 def func():
7     assert 1==1

code analysis

Now based on theoretical analysis and combined with the code, we can roughly calculate that executing the use case from the project root directory should execute 4 valid test cases!

We execute pytest --collect-only in the project root directory to check the situation. We can find that the test_test module 1 and test_test module 2 files are searched, and include TestClass_2 and TestClass_1 classes, internal test_class_3 and test_class_1 and external test functions. test_2,test_1.

D:\pytest search test case rules>pytest --collect-only
============================= test session starts === ==========================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7 .1
rootdir: D:\pytest search test case rules, inifile:
collected 4 items
<Package 'D:\\pytest search test case rules\\test case directory 1'>
  <Module 'test_test module 1.py'>
    <Function 'test_2'>
    <Class 'TestClass_2'>
      <Instance '()'>
        <Function 'test_class_3'>
  <Module 'test_test module 2.py'>
    <Function 'test_1'>
    <Class 'TestClass_1'>
      <Instance '()'>
        <Function 'test_class_1'>

======================== no tests ran in 0.14 seconds =========================

We can roughly see the search rules of pytest. Now let's execute all the use cases and use the command pytest -v. It can be seen that only 4 use cases have been executed, that is, only 4 use cases have been identified. According to the output information below, you can see the location of each use case.

D:\pytest search test case rules>pytest -v
============================= test session starts ====== =======================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 - - c:\python36\python.exe
cachedir: .pytest_cache
rootdir: D:\pytest search test case rules, inifile:
collected 4 items

test case directory 1/test_test module 1.py::test_2 PASSED [ 25%]
test Test case directory 1/test_test module 1.py::TestClass_2::test_class_3 PASSED [50%]
Test case directory 1/test_test module 2.py::test_1 PASSED [75%]
Test case directory 1/test_test Module 2.py::TestClass_1::test_class_1 PASSED [100%]

========================== 4 passed in 0.07 seconds ==== =======================

You can try modifying the files, functions, and class names yourself, and then execute the use case to see if the use case will be executed according to your own ideas!

Summarize

Ok, through these two articles, we probably already know how to name test modules, test classes, test functions, and how to execute test cases using a simple command line. So hurry up and give it a try!

 


If you want to learn automated testing, then the following set of videos should help you a lot 

How to force yourself to finish learning automated testing in one month, and get a job immediately after learning. Even a novice can get it at his fingertips. You can take it away without any thanks, and you are allowed to have sex for free...

Finally, I will share with you some documents and learning materials that I have accumulated and organized. If you need them, you can just get them directly. The above content should be the

most comprehensive and complete preparation warehouse for software testing friends. For the better To organize each module carefully, I also referred to many high-quality blog posts and projects on the Internet, trying not to miss any knowledge point. Many friends relied on these contents to review and got offers from major manufacturers such as BATJ. This warehouse has also helped I have learned a lot about software testing, and I hope it can help you too.

Guess you like

Origin blog.csdn.net/weixin_54696666/article/details/133172543