背景:
windows下,pytest运行测试主要有2种方式,分别是通过cmd运行,和通过pthon解释器运行。
通过CMD运行:
首先,启动cmd.exe,cd到用例文件夹所在的目录,然后可以执行以下命令来运行pytest.
- pytest
- py.test
- python -m pytest
示例:
D:\Python3.7\project\pytest>pytest
================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 2 items
test_case\test_func.py .F [100%]
====================================================== FAILURES =======================================================
________________________________________________ test_add_by_func_aaa _________________________________________________
def test_add_by_func_aaa():
a = 4
b = 6
> assert add(a,b) == 9
E assert 10 == 9
E + where 10 = add(4, 6)
test_case\test_func.py:14: AssertionError
============================================= 1 failed, 1 passed in 0.10s =============================================
D:\Python3.7\project\pytest>py.test
================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 2 items
test_case\test_func.py .F [100%]
====================================================== FAILURES =======================================================
________________________________________________ test_add_by_func_aaa _________________________________________________
def test_add_by_func_aaa():
a = 4
b = 6
> assert add(a,b) == 9
E assert 10 == 9
E + where 10 = add(4, 6)
test_case\test_func.py:14: AssertionError
============================================= 1 failed, 1 passed in 0.12s =============================================
D:\Python3.7\project\pytest>python -m pytest
================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 2 items
test_case\test_func.py .F [100%]
====================================================== FAILURES =======================================================
________________________________________________ test_add_by_func_aaa _________________________________________________
def test_add_by_func_aaa():
a = 4
b = 6
> assert add(a,b) == 9
E assert 10 == 9
E + where 10 = add(4, 6)
test_case\test_func.py:14: AssertionError
============================================= 1 failed, 1 passed in 0.10s =============================================
D:\Python3.7\project\pytest>
以上3个命令,效果都是一样的,可根据个人习惯选择使用。
当有参数时,通过cmd运行pytest,就是把参数依次加到命令后面,使用空格分隔:
示例:pytest -v -k aaa
D:\Python3.7\project\pytest>pytest -v -k aaa
================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- d:\python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 2 items / 1 deselected / 1 selected
test_case/test_func.py::test_add_by_func_aaa FAILED [100%]
====================================================== FAILURES =======================================================
________________________________________________ test_add_by_func_aaa _________________________________________________
def test_add_by_func_aaa():
a = 4
b = 6
> assert add(a,b) == 9
E assert 10 == 9
E -10
E +9
test_case\test_func.py:14: AssertionError
=========================================== 1 failed, 1 deselected in 0.10s ===========================================
D:\Python3.7\project\pytest>
通过Python解释器运行
pytest.main([])
没有任何参数就是这样的格式,如果有参数,则参数按照列表的格式填写。
没有参数意味着pytest按照默认搜索规则执行用例。
带参数运行示例:
import pytest
if __name__ == '__main__':
pytest.main(['-v','./test_case/test_func.py::test_add_by_func_aaa'])
‘’‘
stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 1 item
test_case/test_func.py::test_add_by_func_aaa FAILED [100%]
================================== FAILURES ===================================
____________________________ test_add_by_func_aaa _____________________________
def test_add_by_func_aaa():
a = 4
b = 6
> assert add(a,b) == 9
E assert 10 == 9
E -10
E +9
test_case\test_func.py:14: AssertionError
============================== 1 failed in 0.08s ==============================
[Finished in 1.4s]
’‘’