Preface
- There are currently two testing frameworks for pure testing, pytest and unittest.
- Unittest should be well known, and it is an old framework. Many people use it for automation, whether it is UI or interface.
- pytest is another more advanced and easier-to-use unit testing framework developed based on unittest.
- Whether you go out for an interview or talk to others, pytest is obviously more powerful than unittest.
Why use Pytest
According to the official website of pytest, it has the following characteristics:
- Very easy to get started, simple to get started, rich documentation, there are many examples in the documentation for reference
- Able to support simple unit testing and complex functional testing
- Support parameterization
- During the test execution process, you can skip certain tests, or mark certain cases that are expected to fail as failures.
- Supports repeated execution (rerun) failure cases
- Supports running test cases written by nose and unittest
- Can generate html reports
- Convenient sustainable integration tool jenkins integration
- Can support execution of some use cases
- Has many third-party plug-ins and can be customized and extended
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036
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
quick start
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ =
__Time__ = 2020-04-06 12:33
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
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")
Then, cmd enters the current file directory and executes it directly
pytest
Knowledge points
- If you only execute pytest, it will search for files starting with test_*.py or *_test.py 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
- Adding -q displays simple results: pytest -q start.py
Design principles for Pytest use cases
When writing test cases with Pytest, you must follow the following rules, otherwise test cases that do not comply with the rules will not be executed.
- File names begin with test_*.py files and *_test.py
- Functions starting with test_
- Classes starting with Test cannot contain the __init__ method
- Methods in classes starting with test_
- All packages must have an __init__.py file
Pytest execution use case rules
Note that the following is all about executing the pytest command 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 and test a method in the class
pytest start.py::TestClass::test_two
5. -m mark expression (explained later)
pytest -m login
All tests decorated with the @pytest.mark.login decorator will be run. We will expand on the mark later.
6. -q simple printing, only prints the execution results of test cases
pytest -q start.py
7. -s detailed printing
pytest -s start.py
8. -x stops testing when an error is encountered
pytest start.py -x
9. —maxfail=num, when the number of use case errors reaches the specified number, stop testing
pytest start.py --maxfail=1
10. -k matches use case name
Execute all test cases whose test case name contains http
pytest -s -k http start.py
11. -k excludes certain use cases based on use case names
1 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 always use cmd to run use cases? Now let’s take a look at how to run Pytest in Pycharm.
- First, we need to go to settings and set the unit testing framework to Pytest.
- If it is nosetests, right-click to run it as a python script.
- If unittest is set, it will be run with the unittest framework.
Notice
pytest is compatible with unittest scripts, and the unittest use cases written before can also be run using the pytest framework.
Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!
Software Testing Interview Document
We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.