Pytest运行测试子集

一、单个目录运行

	运行单个目录下的所有测试,以目录作为pytest的参数即可
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest ch2 --tb=no
================================================================================== test session starts ===================================================================================
collected 9 items                                                                                                                                                                         

ch2\test_eight.py ..                                                                                                                                                                [ 22%]
ch2\test_nine.py ..                                                                                                                                                                 [ 44%]
ch2\test_seven.py ..                                                                                                                                                                [ 66%]
ch2\test_six.py .                                                                                                                                                                   [ 77%]
ch2\test_ten.py ..                                                                                                                                                                  [100%]

=================================================================================== 9 passed in 0.12s ====================================================================================

二、单个测试文件/模块运行

	运行单个文件里的全部测试,以路径加文件名作为pytest参数即可
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest ch2/test_ten.py
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

ch2\test_ten.py ..                                                                                                                                                                  [100%]

=================================================================================== 2 passed in 0.06s ====================================================================================

三、单个测试函数运行

	运行单个测试函数,只需要在文件后面添加 :: 符号和函数名即可
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest -v ch2/test_six.py::test_01_six_pass
================================================================================== test session starts ===================================================================================
collected 1 item                                                                                                                                                                          

ch2/test_six.py::test_01_six_pass PASSED                                                                                                                                            [100%]

=================================================================================== 1 passed in 0.04s ====================================================================================

四、运行单个测试类

	测试类用于将某些相识的测试函数/用例组合在一起
class TestTen:
    expected_result = [i for i in range(1, 5)]

    def test_01_ten_pass(self):
        actual_result = tuple(i for i in range(1, 5))
        assert actual_result != self.expected_result

    def test_02_ten_fail(self):
        actual_result = tuple(i for i in range(1, 5))
        assert actual_result != self.expected_result
	要运行该类,在文件后面加上 : : 符号和类名(与运行单个测试函数类似)
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest -v ch2/test_ten.py::TestTen
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

ch2/test_ten.py::TestTen::test_01_ten_pass PASSED                                                                                                                                   [ 50%]
ch2/test_ten.py::TestTen::test_02_ten_fail PASSED                                                                                                                                   [100%]

=================================================================================== 2 passed in 0.03s ====================================================================================

五、单个测试类中的测试方法/用例

	不希望运行测试类中的所有测试方法/用例,只想指定运行其中的一个,可以在文件后面添加 : : 符号和方法名即可
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest -v ch2/test_ten.py::TestTen::test_01_ten_pass
================================================================================== test session starts ===================================================================================
collected 1 item                                                                                                                                                                          

ch2/test_ten.py::TestTen::test_01_ten_pass PASSED                                                                                                                                   [100%]

=================================================================================== 1 passed in 0.02s ====================================================================================

六、用测试名划分测试集合

	-k 选项允许一个表达式指定需要运行的测试,该表达式可以匹配测试名,表达式中可以包含 and、or、not 。

	下面运行的是包含 _pass 的测试:
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest -v -k _pass
================================================================================== test session starts ===================================================================================
collected 16 items / 7 deselected / 9 selected                                                                                                                                            

test_three.py::TestThree::test_01_three_pass XPASS                                                                                                                                  [ 11%]
test_three.py::TestThree::test_03_four_pass PASSED                                                                                                                                  [ 22%]
test_two.py::test_run_pass SKIPPED                                                                                                                                                  [ 33%]
ch1/test_five.py::TestFive::test_01_five_pass PASSED                                                                                                                                [ 44%]
ch2/test_eight.py::TestEight::test_01_eight_pass PASSED                                                                                                                             [ 55%]
ch2/test_nine.py::TestNine::test_01_nine_pass PASSED                                                                                                                                [ 66%]
ch2/test_seven.py::TestSeven::test_01_seven_pass PASSED                                                                                                                             [ 77%]
ch2/test_six.py::test_01_six_pass PASSED                                                                                                                                            [ 88%]
ch2/test_ten.py::TestTen::test_01_ten_pass PASSED                                                                                                                                   [100%]

================================================================= 7 passed, 1 skipped, 7 deselected, 1 xpassed in 0.28s ==================================================================
	下面运行的是包含 _pass和_run 的测试

(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest -v -k "_pass and _run"
================================================================================== test session starts ===================================================================================
collected 16 items / 15 deselected / 1 selected                                                                                                                                           

test_two.py::test_run_pass SKIPPED                                                                                                                                                  [100%]

=========================================================================== 1 skipped, 15 deselected in 0.07s ============================================================================
	下面运行的是不包含_pass和_run 的测试
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest -v -k "_pass and not _run"
================================================================================== test session starts ===================================================================================
collected 16 items / 8 deselected / 8 selected                                                                                                                                            

test_three.py::TestThree::test_01_three_pass XPASS                                                                                                                                  [ 12%]
test_three.py::TestThree::test_03_four_pass PASSED                                                                                                                                  [ 25%]
ch1/test_five.py::TestFive::test_01_five_pass PASSED                                                                                                                                [ 37%]
ch2/test_eight.py::TestEight::test_01_eight_pass PASSED                                                                                                                             [ 50%]
ch2/test_nine.py::TestNine::test_01_nine_pass PASSED                                                                                                                                [ 62%]
ch2/test_seven.py::TestSeven::test_01_seven_pass PASSED                                                                                                                             [ 75%]
ch2/test_six.py::test_01_six_pass PASSED                                                                                                                                            [ 87%]
ch2/test_ten.py::TestTen::test_01_ten_pass PASSED                                                                                                                                   [100%]

======================================================================= 7 passed, 8 deselected, 1 xpassed in 0.09s =======================================================================
	下面运行是包含_pass或_run 的测试
(venv_dome) F:\TESTING\BlogPosts\ReadPytest>pytest -v -k "_pass or _run"
================================================================================== test session starts ===================================================================================
collected 16 items / 6 deselected / 10 selected                                                                                                                                           

test_three.py::TestThree::test_01_three_pass XPASS                                                                                                                                  [ 10%]
test_three.py::TestThree::test_03_four_pass PASSED                                                                                                                                  [ 20%]
test_two.py::test_run_pass SKIPPED                                                                                                                                                  [ 30%]
test_two.py::test_run_fail PASSED                                                                                                                                                   [ 40%]
ch1/test_five.py::TestFive::test_01_five_pass PASSED                                                                                                                                [ 50%]
ch2/test_eight.py::TestEight::test_01_eight_pass PASSED                                                                                                                             [ 60%]
ch2/test_nine.py::TestNine::test_01_nine_pass PASSED                                                                                                                                [ 70%]
ch2/test_seven.py::TestSeven::test_01_seven_pass PASSED                                                                                                                             [ 80%]
ch2/test_six.py::test_01_six_pass PASSED                                                                                                                                            [ 90%]
ch2/test_ten.py::TestTen::test_01_ten_pass PASSED                                                                                                                                   [100%]

================================================================= 8 passed, 1 skipped, 6 deselected, 1 xpassed in 0.09s ==================================================================

上面简介了如何指定测试文件、目录、类、函数/用例,如何利用-k选项选择部分测试,以上总结如有不当之处,还请提出疑义,后面会加以修改,多谢阅读!

始终相信你的努力,终会在某一天得到回报!!!

Pytest运行测试子集_第1张图片

你可能感兴趣的:(python,pytest,python,软件测试)