The high-rich hands of the unit test industry, Pytest framework, hand handle teaching, from getting started2021-11-30 12:10:43 43 ℃
Pytest has not translated a better use document so far, many English is not very good, and it is still very difficult to read English documents during learning. Originally, I plan to write Pytest's detailed use of documentation, because the time relationship has been put on hold until today, I started writing. This article is the first, mainly introducing the introduction of Pytest, followed by the detailed use tutorial for each function in Pytest.
Pytest is a third-party library in Python. You need to install before use, run the following installation command in the command line:
Pip Insatll Pytest code>
Check if the installation is successful and installed, the command line command is as follows:
pytest --version code>
Execute the above command, you can output version information, then explain the installation is successful.
When we perform the use case by Pytest, Pytest automatically recursively traverses all directories under the path, automatically collects test cases according to the rules of the default cases of defaults in Pytest. All we first need to understand the default use case recognition rules when you use Pytest's test case.
1. Use example file: All files named_ start or _Test starts will be identified as the use case file. 2: Use an example class, the class that starts in each Test in the test file is a test case class. 3. Test case: The method of each Test in the test class is a test case. The function of each Test in the test file is also a test case.
Remarks: The default use case lookup rules can be modified in Pytest configuration files (subsequent chartes will detail the use of configuration files)
In addition, Pytest is compatible with UnitTest, written in the use case written by UnitTest, and Pytest can identify it.
By understanding the rules used in the above-described Pytest, you can know that the use of example in Pytest can use the form of functions. You can also use the form of the class, then you will introduce you to the two ways.
Rules: Take the name of the case to start with TEST
# testcasestest_demo1.pydef test_demo: assert 100 == 100 code>
The test function can be executed using the command pytest, and the output is as follows:
C: Testcases> Pytest Test Session Starts Platform Win32 - Python 3.7.3, Pytest-5.4.2, PYTSTDIR: C: TestcaseSplugins: TestReport-1.1.2collected 1 item test_demo1 .py. [100 1 passed in 0.26s code>
Rules: Test class named Test, use case method starts with TEST
# test_demo2.pyclass testdome: Def test_demo1 (self): assert 11 == 11def test_demo (self): assrt 22 == 21 code>
The command pytest runs the above use case, as follows:
Test session Starts Platform Win32 - Python 3.7.3, Pytest-5.4.2, Py-1.8.0, Pluggy-0.13.0Rootdir: C: TestcaseSplugins: TestReport-1.1.2Collected 2 items test_demo1.py .f [ 100 FAILURES TestDome.test_demo self =
Third, execute test
In the above we use the pytest this command to perform test cases. About PyTest execution test, there are two ways, one is the command line through the pytest this command, and the test can be performed by the Pytest.main method in the code. Next, I will introduce the way Pytest execution test and common parameters.
1, execute parameters
# Test case class testdome: Def test_demo1 (self): print ('---- test case execution') assert 11 == 11 code>
Parameters -V: Detailed parameter information for display
C: Testcases> Pytest -s Test Session Starts Platform Win32 - Python 3.7.3, Pytest-5.4.2, PYTSPLUGINS: TestReport-1.1.2collected 1 Item test_demo1.py::testdome:test_demo1 ---- Test case execution --- Output 1 test case execution --- Output 2 -------- Passed 1 passed in 0.28s code>
Pytest.main method Execute test parameters transmission mode:
So the parameters are placed in the list, each parameter is an element in the list.
pytest.main (['- v', '- s']) code>
Detailed parameters can be viewed using the command pytest -h
3. Specify the test directory executed
Command Pytest Test Directory Path
Pytest TestCase / code>
Pytest will execute all test cases under the specified directory path
Command Pytest test file path
pytest testcase / test_demo1.py code>
Pytest will perform all test cases in the specified test file.
Command Pytest Test File :: Test Class
pytest testcase / test_demo1.py :: testclass code>
Pytest will execute all test cases in the specified test class
Command Pytest Test File :: Test Class :: Test Method
pytest testcase / test_demo1.py :: testclass :: test_method code>
Pytest will execute the specified test method
Basic entry gives you here, from the next article, give you a detailed introduction of Pytest's related features.
Guess you like
- 2022-01-29Welfare | Liugong excavation machine tiger's exclusive red envelope cover!
- 2022-01-28Wang Zi or Vice President of the entry Gree, once again distressed 6 billion Luo Yonghao
- 2022-01-28Baidu Tencent Byte, trillion business, to the turning point?
- 2022-01-28Just add it!Fully solve the Amazon Marketing Plan impact rankings!
- 2022-01-28China Unicom's 2022 "Foshan 5G Yunhua Street" opened!Citizens realize a year-on-year change
- 2022-01-27WeChat iOS8.0.18 official version of new content disclosure: Friend circle supports release 20 photos
- 2022-01-27The shareholder wronned, and ran to Lei Jun Weibo message: it lost three or 40,000, the stock price is all the way to the south ...
- 2022-01-27[Safety Reircle] North America IT Company Survey shows that the lesso software gang is increasing the recruitment of internal staff to assist in attack
- 2022-01-27Tesla strong financial report: US stocks are bleak, nothing to do with me
- 2022-01-27Wing minority CEO Lu Zhen - Profile in promoting regional data interconnection
- Home appliance retail channel fragmentation is just ill: not in dispersing but giants
- The same price bucket machine Redmik40 top with version to hand price 2039 yuan
- After using 7 days of millet 12pro, I decided to change my MATE40PRO?Just because these 2 points
- Tencent "two people" sat in a hundred billion assets, but there were more than 100,000 Bora. How many low-key he has?
- Baidu Tencent Byteriendal Trill Business is a turning point?
- 30 days 1.5 million kilometers, Web telescope arrived at the end of the transfer, 18 mirror
- Did you understand the prefabricated vegetables, prefabricated vegetables in the construction industry?
- | Handwritten Wallpaper
- [Hard Technology Week 3: Dongfang Space Complete Nearly 300 million yuan pre-A round financing, XR game sports capture technology company Refract completes $ 6 million A round financing
- Briefly describe the applicable population of four website managed types