脚本树如下:
test1文件下test_01.py存放test1和test2用例
test1文件下test_02.py存放test1和test2用例
test2文件下test_03.py存放test1和test2用例
test2文件下test_04.py存放test1和test2用例
1、运行所有用例
import pytest
if __name__ == "__main__":
pytest.main(['-s',''])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 8 items
test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.
test1\test_02.py test_02-test1:5
.test_02-test2:5
.
test2\test_03.py test_03-test1:5
.test_04-test2:5
.
test2\test_04.py test04-test1:5
.test04-test2:5
.
============================== 8 passed in 0.25s ==============================
Process finished with exit code 0
--------------------------------------------------------------------------------------
2、运行指定文件夹下用例
import pytest
if __name__ == "__main__":
pytest.main(['-s','test1'])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 4 items
test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.
test1\test_02.py test_02-test1:5
.test_02-test2:5
.
============================== 4 passed in 0.12s ==============================
Process finished with exit code 0
-----------------------------------------------------------------------------------
3、运行指定py文件下用例
import pytest
if __name__ == "__main__":
pytest.main(['-s','test1/test_01.py'])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.
============================== 2 passed in 0.04s ==============================
Process finished with exit code 0
4、运行指定类下的用例
import pytest
if __name__ == "__main__":
pytest.main(['-s','test1/test_01.py::Test'])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.
============================== 2 passed in 0.04s ==============================
Process finished with exit code 0
-------------------------------------------------------------------------------------
5、运行执行方法下的用例
import pytest
if __name__ == "__main__":
pytest.main(['-s','test1/test_01.py::Test::test1'])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 1 item
test1\test_01.py 调用了获取token
test_01-test1:5
.
============================== 1 passed in 0.04s ==============================
Process finished with exit code 0