python--pytest库

 

pytest:是一个框架,使构建简单和可扩展的测试变得容易。

安装:pip install -U pytest

检查安装:pytest --version

官方文档:https://docs.pytest.org/en/latest/contents.html#toc

 

Running pytest can result in six different exit codes:

Exit code 0: All tests were collected and passed successfully
Exit code 1: Tests were collected and run but some of the tests failed
Exit code 2: Test execution was interrupted by the user
Exit code 3: Internal error happened while executing tests
Exit code 4: pytest command line usage error
Exit code 5: No tests were collected

 

 

 

 

 

 

 

 

To stop the testing process after the first (N) failures:

pytest -x            # stop after first failure
pytest --maxfail=2    # stop after two failures

Pytest supports several ways to run and select tests from the command-line.(多种方式运行测试用例)

Run tests in a module:pytest test_mod.py

Run tests in a directory:pytest testing/

Run tests by keyword expressions:pytest -k "MyClass and not method"

This will run tests which contain names that match the given string expression, which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple.

Run tests by node ids:

To run a specific test within a module:pytest test_mod.py::test_func

Another example specifying a test method in the command line:pytest test_mod.py::TestClass::test_method

Run tests by marker expressions:pytest -m slow

Will run all tests which are decorated with the @pytest.mark.slow decorator.

Run tests from packages:pytest --pyargs pkg.testing

This will import pkg.testing and use its filesystem location to find and run tests from.

 

@pytest.fixture(scope="module")
scope作用域: functionclassmodulepackage or sessionfunctionclassmodulepackage or session.

conftest.py: sharing fixture functions

If during implementing your tests you realize that you want to use a fixture function from multiple test files you can move it to a conftest.py file. You don’t need to import the fixture you want to use in a test, it automatically gets discovered by pytest. The discovery of fixture functions starts at test classes, then test modules, then conftest.py files and finally builtin and third party plugins.

详情:https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions

 

以下是方程无解老师整理的内容:

 Unittest:python内嵌的测试框架

编写简范:

  测试模块:import unittest

  测试类必须继承:unittest.TestCase

  测试方法必须必须以“test_”开头

  模块名字、类名不做要求

测试方法级别:setUp、tearDown

测试类级别:setUpClass、tearDownClass

模块级别:setUpModule、tearDownModule

 

更高级的框架:Pytest

  具有很多第三方插件:http://plugincompat.herokuapp.com/

编写规范:

  测试文件以“test_”开头(以"_test"结尾也行)

  测试类以"Test"开头,并且不能带有__init__方法

  测试函数以"test_"开头

 

部分应用:

 1 # encoding: utf-8
 2 
 3 import pytest
 4 
 5 
 6 # pytest:参数化
 7 @pytest.mark.parametrize("x,y", [(3, 3), (3+5, 8), (6*2, 12), ("a", "a")])
 8 def test_add(x, y):
 9     assert x == y
10 
11 
12 value = 0
13 
14 
15 def test_add1():
16     global value
17     value = 10
18     assert value == 10
19 
20 
21 def test_add2():
22     print("I am 2")
23     assert value == 10
24 
25 
26 @pytest.fixture()
27 def login_and_login_out():
28     return 1
29     # print("login start")
30     # yield
31     # print("login out")
32 
33 
34 class TestSample:
35     def test_answer1(self, login_and_login_out):
36         result = login_and_login_out
37         assert result == 1
38 
39     # fixtures名字调用
40     def test_answer2(self, login_and_login_out):
41         result = login_and_login_out
42         assert result == 1
43 
44     # fixtures decorator调用
45     @pytest.mark.usefixtures("login_and_login_out")
46     def test_answer3(self):
47         assert 1 == 1
48 
49 
50 # fixtures autouse调用
51 @pytest.fixture(scope="module", autouse=True)
52 def login():
53     print("login -----------------")
54     yield
55     print("end login -------------")
56 
57 
58 @pytest.fixture(scope="class", autouse=True)
59 def out():
60     print("login out start -------")
61     yield
62     print("login out end ---------")
63 
64 
65 class TestSample2:
66     def test_answer4(self):
67         assert "hello 2019" == "hello 2019 "
68 
69     def test_answer5(self):
70         assert "fine" == "fine "
71 
72 
73 class TestSample3:
74     def test_answer(self):
75         assert "welcome" == "welcome "
View Code

 

 pytest执行用例的方式:

  1)执行一个module:pytest -v src/testcase/api/xxx.py

  2)执行一个类:pytest -v src/testcases/api/xxx.py::TestSample(类名)

  3)执行一个方法:pytest -v src/testcases/api/xxx.py::TestSample(类名)::test_xx(方法名)

  4)执行一个目录或package:pytest -v src/testcases/api

  5)通过标签来运行测试用例:pytest -m P0(标签名) src/testcases/api/

  通过pytest.main运行:pytest.main(['-v', '--instafail', 'testcases/api/xxx.py', '-m=P0'])

  

 

posted on 2019-01-06 21:26  我要的明天  阅读(619)  评论(0编辑  收藏  举报

导航