Pytest参数选项自由执行测试用例详解(二)

  

      运行pytest可以指定目录和文件,如果不指定,pytest会搜索当前目录及其子目录中以test_开头或以_test结尾得测试函数。我们把pytest搜索测试文件和测试用例的过程称为测试搜索(test discovery)。只要遵循pytest的命名规则,pytest就能自动搜索所有待执行的测试用例。所有的包必须要有init.py文件(在使用各种编辑器时会自动生成)

1、测试文件命名规则,test_xxx.py或xxx_test.py

2、方法、测试函数命名规则,test_xxx

3、测试类命名规则,Testxxx,并且不能带有 init 方法

Pytest参数选项在脚本中和命令行用法详解(一)

-k选项  -K EXPRESSION

使用表达式指定某个关键字的测试用例,如果某测试名是唯一的或多个测试名的前缀或后缀相同,可快速匹配,匹配范围是全局相同目录或下层目录所有(包名、文件名、类名、函数名为变量),文件名、类名、函数名,必须是test_开头或_test结尾的。

pytest.main(['-k','关键字'])
关键字包含于或等于文件名、类名、函数名,多个关键字之间用and、or、not连接


测试代码如下:

chengzi\test_04.py


import pytest




class TestClass(object):
    def test_one(self):
        assert 2 == 1




    def test_two(self):
        assert 2 == 2


    def test_two2(self):
        assert 2 == 2

执行匹配包名,运行了3条用例

pytest.main(['-v','-k','chengzi'])

if __name__ == '__main__':
    pytest.main(['-v','-k','chengzi'])
    
 "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Packages': {'pluggy': '0.13.1', 'py': '1.9.0', 'pytest': '6.1.1'}, 'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Python': '3.5.2', 'Plugins': {'allure-pytest': '2.8.18', 'metadata': '1.8.0', 'rerunfailures': '9.1.1', 'html': '1.22.0'}}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 items


test_04.py::TestClass::test_one FAILED                                   [ 33%]
test_04.py::TestClass::test_two PASSED                                   [ 66%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]


================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________


self = 


    def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1


test_04.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
========================= 1 failed, 2 passed in 0.63s =========================


Process finished with exit code 0

执行匹配文件名,运行了3条用例

pytest.main(['-v','-k','test_04.py'])
if __name__ == '__main__':
    pytest.main(['-v','-k','test_04.py'])
    
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Platform': 'Windows-10-10.0.18362-SP0', 'Python': '3.5.2', 'Packages': {'py': '1.9.0', 'pluggy': '0.13.1', 'pytest': '6.1.1'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Plugins': {'rerunfailures': '9.1.1', 'html': '1.22.0', 'allure-pytest': '2.8.18', 'metadata': '1.8.0'}}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 items


test_04.py::TestClass::test_one FAILED                                   [ 33%]
test_04.py::TestClass::test_two PASSED                                   [ 66%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]


================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________


self = 


    def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1


test_04.py:6: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
========================= 1 failed, 2 passed in 0.48s =========================


Process finished with exit code 0

执行匹配类名,运行了3条用例

pytest.main(['-v','-k','Class'])


if __name__ == '__main__':
    pytest.main(['-v','-k','Class'])


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Plugins': {'html': '1.22.0', 'rerunfailures': '9.1.1', 'metadata': '1.8.0', 'allure-pytest': '2.8.18'}, 'Packages': {'pluggy': '0.13.1', 'py': '1.9.0', 'pytest': '6.1.1'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Python': '3.5.2', 'Platform': 'Windows-10-10.0.18362-SP0'}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.

你可能感兴趣的:(软件测试,java,linux,编程语言,python)