In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people don't know what to do about how the pytest command line in Python works. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Preface
Calling use cases on the command line is the most commonly used way, which is indeed much better than java's TestNG framework, at least without writing xml files. In order to provide a way to customize running use cases, pytest provides a number of run commands for customizing a certain type of test case or a certain test case, etc.
Command Line run use case in pycharm
How to run after writing test cases in pycharm? Pycharm doesn't seem to be executed with one click like the plug-ins for TestNG in eclipse, so we can do it on the command line, as shown in the following figure as a use case file:
The code is as follows:
#-*-coding: utf-8-*-import pytestclass Test_simple (): @ pytest.mark.test def test_case1 (self): print ("testCase1") tof = True assert tof @ pytest.mark.normal @ pytest.mark.test def test_case2 (self): print ("testCase2") tof = False assert tof def test_case3 (self): print ("testCase3") assert True @ pytest.mark.test def setup_class (self): print ("for test groups") @ pytest.mark.normal def setup_class (self): print ("for normal groups")
As shown above, a project called testSimple has been added with some test cases, namely Test_simple
You can open the Terminal window below when you want to run the use case:
It will automatically switch to the current project directory, and then you can use the command of pytest. The following is a brief description of the running result:
Use pytest in the terminal
The use of pytest in a terminal is also similar to that in pycharm. Take the windows system as an example:
Change to the project or directory where the use case resides, and then run pytest, as shown below:
The same method is used in linux systems, except that if you do not add a soft connection to pytest, you need to precede the pytest with the python command
All use cases run
When all runs, you don't need to add any suffixes, just add the command pytest. At this time, the printed information is relatively simple:
E:\ pyspace\ testSimple > pytest= test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 3 items Testcase\ Test_simple.py. F. [100%] = FAILURES = _ _ Test_simple.test_case2 _ _ _ self = @ pytest.mark.normal @ pytest.mark.test def test_case2 (self): print ("testCase2") tof = False > assert tofE assert Falsetestcase\ Test_simple.py:18: AssertionError-- -Captured stdout call- -testCase2= 1 failed 2 passed in 0.08 seconds = E:\ pyspace\ testSimple > print details-v
As shown in the figure above, it only shows the success or failure of the use case, but the log in it is not printed, so what if we want to see the running details? You can add the-v tag, as follows:
E:\ pyspace\ testSimple > pytest- v = test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0-- e:\ software\ python\ python3.7\ python.execachedir: .pytest _ cachemetadata: {'Python':' 3.7.1, 'Platform':' Windows-7-6.1.7601, 'Packages': {' pytest': '4.4.1' 'py':' 1.8.0, 'pluggy':' 0.11.0}, 'Plugins': {' metadata': '1.8.0,' html': '1.20.0,' allure-pytest': '2.6.3'},' JAVA_HOME':'D:\\ project\\ jdk1.8'} rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 3 items Testcase/Test_simple.py::Test_simple::test_case1 PASSED [33%] testcase/Test_simple.py::Test_simple::test_case2 FAILED [66%] testcase/Test_simple.py::Test_simple::test_case3 PASSED [100%] = FAILURES = _ _ Test_simple.test_case2 _ _ _ self = @ pytest.mark.normal @ pytest.mark.test def test_case2 (self): print ("testCase2") tof = False > assert tofE assert Falsetestcase\ Test_simple.py:18: AssertionError -Captured stdout call- -testCase2= 1 failed 2 passed in 0.08 seconds = E:\ pyspace\ testSimple >
As shown above, all the details will be printed out.
Designated group
If the use case contains multiple groups and you want to run only one of the groups, use-m "group name" and still use the code as above, and run the command and the result as follows:
E:\ pyspace\ testSimple > pytest- s-m "normal" = test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 3 items / 2 deselected / 1 selected Testcase\ Test_simple.py is used for normal group testCase2F= FAILURES = _ _ Test_simple.test_case2 _ _ _ self = @ pytest.mark.normal @ pytest.mark.test def test_case2 (self): print ("testCase2") tof = False > assert tofE assert Falsetestcase\ Test_simple.py:18: AssertionError= 1 failed 2 deselected in 0.07 seconds = E:\ pyspace\ testSimple > use expressions to specify certain use cases-k
The-k option allows us to set expressions to run certain use cases. The following parameters only run test_case1 and test_case2
E:\ pyspace\ testSimple > pytest- v-k "case1 or case2" = test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0-- e:\ software\ python\ python3.7\ python.execachedir: .pytest _ cachemetadata: {'Python':' 3.7.1, 'Platform':' Windows-7-6.1.7601, 'Packages': {' pytest': '4.4.1' 'py':' 1.8.0, 'pluggy':' 0.11.0}, 'Plugins': {' metadata': '1.8.0,' html': '1.20.0,' allure-pytest': '2.6.3'},' JAVA_HOME':'D:\\ project\\ jdk1.8'} rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 3 items / 1 deselected / 2 selected Testcase/Test_simple.py::Test_simple::test_case1 PASSED [50%] testcase/Test_simple.py::Test_simple::test_case2 FAILED [100%]
There are many ways to write expressions. You can use the full name such as test_case1, or you can remove test_,. In addition to or, you can also use not to specify that those use cases do not run.
Stop running if you encounter a failure-x
The original running rule of pytest is to execute every use case, regardless of whether there is a failure or not. if we want to stop when the use case fails, we can use-x, as shown below, and do not run the third use case after the second use case fails:
E:\ pyspace\ testSimple > pytest- v-x = test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0-- e:\ software\ python\ python3.7\ python.execachedir: .pytest _ cachemetadata: {'Python':' 3.7.1, 'Platform':' Windows-7-6.1.7601, 'Packages': {' pytest': '4.4.1' 'py':' 1.8.0, 'pluggy':' 0.11.0}, 'Plugins': {' metadata': '1.8.0,' html': '1.20.0,' allure-pytest': '2.6.3'},' JAVA_HOME':'D:\\ project\\ jdk1.8'} rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 3 items Testcase/Test_simple.py::Test_simple::test_case1 PASSED [33%] testcase/Test_simple.py::Test_simple::test_case2 FAILED [66%] = FAILURES = _ _ Test_simple.test_case2 _ _ _ self = @ pytest.mark.normal @ pytest.mark.test def test_case2 (self): print ("testCase2") tof = False > assert tofE assert Falsetestcase\ Test_simple.py:18: AssertionError-- -Captured stdout call-- -testCase2= 1 failed 1 passed in 0.08 seconds = E:\ pyspace\ testSimple > specify to run a test py file
To specify to run a py file, you only need to connect the relative path to the file:
E:\ pyspace\ testSimple > pytest- v testcase/Test_example.py= test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0-- e:\ software\ python\ python3.7\ python.execachedir: .pytest _ cachemetadata: {'Python':' 3.7.1', 'Platform':' Windows-7-6.1.7601 UV SP1 trees, 'Packages': {' pytest': '4.4.1' 'py':' 1.8.0, 'pluggy':' 0.11.0}, 'Plugins': {' metadata': '1.8.0,' html': '1.20.0,' allure-pytest': '2.6.3'},' JAVA_HOME':'D:\\ project\\ jdk1.8'} rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 1 item Testcase/Test_example.py::Test_example::test_aaa PASSED ] = 1 passed in 0.02 seconds = E:\ pyspace\ testSimple > specify to run a class
Write as follows: py file path:: class name. Examples are as follows:
E:\ pyspace\ testSimple > pytest- v testcase/Test_example.py::Test_example2= test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0-- e:\ software\ python\ python3.7\ python.execachedir: .pytest _ cachemetadata: {'Python':' 3.7.1, 'Platform':' Windows-7-6.1.7601After SP1' 'Packages': {' pytest': '4.4.1,' py': '1.8.0,' pluggy': '0.11.0},' Plugins': {'metadata':' 1.8.0, 'html':' 1.20.0, 'allure-pytest':' 2.6.3'} 'JAVA_HOME':' D:\\ project\\ jdk1.8'} rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 1 item Testcase/Test_example.py::Test_example2::test_bbb PASSED ] = 1 passed in 0.08 seconds = E:\ pyspace\ testSimple > specify a method to run:
Write as follows: py file path:: class name:: method name. Examples are as follows:
E:\ pyspace\ testSimple > pytest- v testcase/Test_example.py::Test_example2= test session starts = platform win32-- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0-- e:\ software\ python\ python3.7\ python.execachedir: .pytest _ cachemetadata: {'Python':' 3.7.1, 'Platform':' Windows-7-6.1.7601After SP1' 'Packages': {' pytest': '4.4.1,' py': '1.8.0,' pluggy': '0.11.0},' Plugins': {'metadata':' 1.8.0, 'html':' 1.20.0, 'allure-pytest':' 2.6.3'} 'JAVA_HOME':' D:\\ project\\ jdk1.8'} rootdir: e:\ pyspace\ testSimpleplugins: metadata-1.8.0, html-1.20.0 Allure-pytest-2.6.3collected 1 item Testcase/Test_example.py::Test_example2::test_bbb PASSED ] = 1 passed in 0.08 seconds = E:\ pyspace\ testSimple >
Such as the above several can also be used in combination
Other
Pytest also contains many other uses, which can be viewed using pytest-- help, as follows:
After reading the above, have you mastered how the pytest command line mode in Python works? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.