- There are currently two pure test testing frameworks, pytest and unittest
- unittest should be widely known, and it is also an old framework. Many people use it for automation, whether it is UI or interface
- pytest is another more advanced and useful unit testing framework developed based on unittest
Why use Pytest
According to the official website of pytest, it has the following characteristics:
- Very easy to use, easy to get started, rich in documentation, and there are many examples in the documentation for reference
- Able to support simple unit tests and complex functional tests
- Support parameterization
- During the execution of the test, some tests can be skipped (skip), or some expected failure cases can be marked as failure
- Support repeated execution (rerun) failure case
- Support running test cases written by nose, unittest
- Can generate html report
- Convenient and continuous integration tool jenkins integration
- Some use cases can be supported
- Has many third-party plug-ins, and can be customized
Install pytest
cmd run
pip install -U pytest
pip3 install pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
view version
pytest --version
the code
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert hasattr(x, "check")
knowledge points
- If only pytest is executed, it will search for the test_*.py or *_test.py file in the current directory and its subdirectories. After finding the file, find the function starting with test in the file and execute it
- If you only want to execute a certain file, you can
-
pytest start.py
-
- Add -q to display simple results:
-
pytest -q start.py
-
Design Principles of Pytest Use Cases
When writing test cases with Pytest, be sure to follow the rules below, otherwise test cases that do not conform to the rules will not be executed
- Filenames start with test_*.py files and *_test.py
- functions starting with test_
- Classes beginning with Test cannot contain __init__ methods
- Methods in classes starting with test_
- All package packages must have __init__.py file
Pytest executes use case rules
Note that the following are all about executing pytest commands in cmd
1. All use cases in a certain directory
pytest
2. Execute the use case under a certain py file
pytest 脚本名称.py
3. Run a function in the start.py module, or a class, or a method in a class
You can add v or not add -v, if you add -v, the printed information will be more detailed
pytest -v 08_mark.py::TestClass::test_method
pytest 08_mark.py::TestClass::test_method
pytest start.py::test_answer
4. Run the start.py module to test a method in the class
pytest start.py::TestClass::test_two
5. -m mark expression (following explanation)
pytest -m login
Will run all tests decorated with the @pytest.mark.login decorator, and we will expand on marking later
6. -q simple printing, only print the execution result of the test case
pytest -q start.py
7. -s print in detail
pytest -s start.py
8. -x Stop the test when an error is encountered
pytest start.py -x
9. --maxfail=num, when the number of use case errors reaches the specified number, stop the test
pytest start.py --maxfail=1
10. -k matches the use case name
Execute all test cases whose test case name contains http
pytest -s -k http start.py
11. -k Exclude certain use cases based on the use case name
pytest -s -k "not http" start.py
12. -k matches different use case names at the same time
pytest -s -k "method or weibo" start.py
Pycharm runs Pytest
We usually write code in Pycharm, how can we use cmd to run the use cases all the time, now let's take a look at how to run Pytest in Pycharm
- First of all, we first go to the settings to set the unit test framework as Pytest
- If it is nosetests, right-click to run is run by a python script
- If unittest is set, it will run with the unittest framework