1, the introduction of

Pytest is a full-fledged Python testing tool that helps you write better programs.

Pytest is a framework that makes it easy to build simple and scalable tests. Tests are expressive and readable, without boilerplate code. After a few minutes, you can start small unit tests or complex functional tests of your application or library.

 

Features:

1, very easy to get started, simple to get started, the document is rich, there are many examples in the document can refer to.

2, can support simple unit testing and complex functional testing.

3. Support parameterization.

4. Some tests can be skipped during test execution, or cases that are expected to fail can be marked as failures.

5, support to reruns failed case.

6, support to run test cases written by Nose and UnitTest.

7, can generate HTML report.

8. It can be well combined with CI tools, such as Jenkins.

9. Support partial use cases.

10, there are many third-party plug-ins, and you can customize the extension.

 

Official website:

pytest.org/

 

Making web site:

Github.com/pytest-dev/…

 

2, installation,

1. On the CLI, run the following command to install:

pip install -U pytest
Copy the code

 

Or (Use domestic Douban source, data will be regularly synchronized with foreign official websites, fast.)

pip install pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
Copy the code

 

2. Check the installed version:

pytest --version
Copy the code

 

3. Quick start

Create test_demo.py file.

Script code:

#! /usr/bin/env python # -* -coding: utf-8 -*- "" 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")Copy the code

 

2. Open the command line, go to the current directory, and enter pytest (or py.test is optional) to execute the command.

 

 

Conclusion:

Py or *_test.py files in the current directory and its subdirectories will be searched. After finding the files, the function starting with test will be executed.

If you only want to execute a file pyTest test_demo.py

Pytest -q test_demo.py = test_demo.py

 

3.1 pyTest design use-case rules

Write according to the following rules, otherwise test cases that do not conform to the rules will not be executed.

1. File names are test_*. Py and *_test.py.

2. Functions starting with test_.

3. Classes starting with Test cannot contain init methods.

Methods in classes starting with test_.

All packages must have an init.py file.

 

Pytest implements use case rules

Execute the Pytest command on the command line

3.2.1. Execute all use cases in the directory

Pytest or py. TestCopy the code

 

3.2.2. Execute a single PyTest module

Pytest File name.pyCopy the code

 

3.2.3 Run a class in a module

Pytest file name. Py :: Class nameCopy the code

 

3.2.4 Run a method in a class in a module

Pytest file name. Py :: class name :: method nameCopy the code

 

3.2.5. -v Printing Run Log Information (Details)

Pytest -v File name.pyCopy the code

 

3.2.6 -q Displays Run Log Information (brief)

Pytest -q File name.pyCopy the code

 

3.2.7 -s Console output results

Pytest -v -s Specifies the name of the file. Py s contains console output and detailed run logsCopy the code

 

3.2.8 -m tag expression

Pytest -M Login will run all tests decorated with the @Pytest.mark.Login decoratorCopy the code

 

3.2.9 If the -x case fails to run, the execution will be stopped immediately

Pytest-x file name.pyCopy the code

 

3.2.10. -k Run the use case containing the keyword

Pytest -v -k "one" filene. py Execution test case name contains all test cases of oneCopy the code

 

3.2.11. -k Run the use case of excluding keywords

Pytest -v -k "not one" filenames. Py Execution test case names do not contain all test cases of oneCopy the code

 

3.2.12. -k Runs the use case matching multiple keywords

Pytest -v -k "one or two" filene. py The name of the test case contains all the cases with one or twoCopy the code

 

3.2.13. –maxfail=num Stop the test when the number of errors reaches the specified number

Pytest filenames. Py -- maxFail =1 Maximum number of failures allowed when a use case is run.Copy the code

AllTests software tests

Share software testing, automated testing, performance testing, test development and other technical articles and resources, welcome to pay attention to!