pytest之运行测试

背景:

windows下,pytest运行测试主要有2种方式,分别是通过cmd运行,和通过pthon解释器运行。

通过CMD运行:

首先,启动cmd.exe,cd到用例文件夹所在的目录,然后可以执行以下命令来运行pytest.

  1. pytest
  2. py.test
  3. 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]
’‘’	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值