Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pytest in Python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Python how to use Pytest, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

2. Pytest installation

Import pytest class Test_class: def test_001 (self): print ('use case 001') assert 8 = = 8 def test_002 (self): print (' use case 002') assert 6 = = 6 def test_003 (self): print ('use case 003') assert 3 = = 2 if _ name_ _ = "_ _ main__": # parameters need to be list If multiple parameters are put in list, there will be no warning # pytest.main ('- Q test_class.py') pytest.main (['- Q test_class.py', 'test_class.py'])

3. Create a simple test case

Import pytest class Test_class: def test_001 (self): print ('use case 001') assert 8 = = 8 def test_002 (self): print (' use case 002') assert 6 = = 6 def test_003 (self): print ('use case 003') assert 3 = = 2 if _ name_ _ = "_ _ main__": # parameters need to be list If multiple parameters are put in list, there will be no warning # pytest.main ('- Q test_class.py') pytest.main (['- Q test_class.py', 'test_class.py'])

Execution result

The checkpoint of the use case in pytest is asserted directly with Python's assert.

The expression after assert results in True, which means that the checkpoint passes, and the result is False, which means that the checkpoint fails.

To execute the test, we just need to run py.test in the same directory as the test file test_class. Pytest will look in the current directory for files that start with test (that is, test files). After finding the test files, pytest will go to the test files to find the test functions that start with test_ and execute them.

4. Pycharm sets Pytest

# file- > Setting- > Tools- > Python Integrated Tools- > Project name-> Default test runner- > right select py.test#, select pytest to run or run .py file directly

Execution result

As can be seen from the above: Pytest is compatible with UnitTest scripts, and previously written UnitTest use cases can also be run using the Pytest framework.

5. Pytest automatically generates reports

# need to install pytest-html > pip install pytest_html # generate reports in html format > pytest- v test_1.py-- html=Path # generate reports in xml format > pytest- v test_1.py-- junitxml=Path # generate reports in txt format > pytest- v test_1.py-- resultlog=Path

Note: check whether the path (root directory) is correct when running the instruction

Reference address:

Https://docs.pytest.org/en/latest/warnings.html

Generate a report .test _ class.py::Test_class::test_001 .test _ class.py::Test_class::test_002 F test_class.py::Test_class::test_003 self =

< test_class.Test_class object at 0x000001582B868978 >

Def test_003 (self): print ('use case 003') > assert 3 = = 2 E assert 3 = = 2 E + 3 E-2 test_class.py: 24: AssertionError, is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report