欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python教程之pytest命令行方式运行用例

程序员文章站 2022-03-08 20:09:58
目录前言pycharm里命令行运行用例终端中使用pytest用例全部运行打印详情-v指定组别使用表达式指定某些用例-k遇到失败即停止运行-x指定运行某个测试py文件指定运行某个class指定运行某个方...

前言

用命令行方式调用用例是我们最常用的方式,这方面确实比java的testng框架要好用许多,至少不用写xml文件,为了提供定制化运行用例的方式,pytest提供了许多运行命令以供定制化运行某一类测试用例或者某个测试用例等;

pycharm里命令行运行用例

在pycharm里写好了测试用例后如何运行呢?pycharm里好像并没有像eclipse里提供testng用的插件一样可以一键执行的方式,那么我们可以使用命令行的方式来进行,如下图所示为一个用例文件:

Python教程之pytest命令行方式运行用例

代码如下:

#-*- coding: utf-8 -*-
import pytest


class test_simple():

    @pytest.mark.test
    def test_case1(self):
        print("testcase1")
        tof = true
        assert tof

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testcase2")
        tof = false
        assert tof

    def test_case3(self):
        print("testcase3")
        assert true

    @pytest.mark.test
    def setup_class(self):
        print("用于test组")

    @pytest.mark.normal
    def setup_class(self):
        print("用于normal组")

如上所示添加了一个名为testsimple的工程,内添加了一些测试用例即test_simple;

想要运行用例时可以打开下方的terminal窗口:

Python教程之pytest命令行方式运行用例

会自动切换到当前工程目录下,而后即可使用pytest的命令了,如下对运行结果简单做下说明:

Python教程之pytest命令行方式运行用例

终端中使用pytest

在终端中使用pytest也是和在pycharm中类似,如下以windows系统为例:

先切换到用例所在工程或者目录而后运行pytest即可,如下:

Python教程之pytest命令行方式运行用例

linux系统中也是同样的使用方法,只是如果没有为pytest添加软连接,则需要在pytest前面加上python命令;

用例全部运行

全部运行时不需要添加任何后缀,只需要添加命令pytest即可,此时打印的信息比较简单:

e:\pyspace\testsimple>pytest
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items                                                                                                                                                                                                                                                       

testcase\test_simple.py .f.                                                                                                                                                                                                                                       [100%]

=============================================================================================================================== failures ===============================================================================================================================
________________________________________________________________________________________________________________________ test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.test_simple.test_simple object at 0x00000000038508d0>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testcase2")
        tof = false
>       assert tof
e       assert false

testcase\test_simple.py:18: assertionerror
------------------------------------------------------------------------------------------------------------------------- captured stdout call -------------------------------------------------------------------------------------------------------------------------
testcase2
================================================================================================================== 1 failed, 2 passed in 0.08 seconds ==================================================================================================================

e:\pyspace\testsimple>

打印详情-v

如上图所示,只显示了用例时成功还是失败,至于里边的log则没有打印,那么如果我们想要看运行详细信息怎么办呢?可以加上-v标签,如下:

e:\pyspace\testsimple>pytest -v
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'python': '3.7.1', 'platform': 'windows-7-6.1.7601-sp1', 'packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'java_home': 'd:\\project\\jdk1.8'}
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items                                                                                                                                                                                                                                                       

testcase/test_simple.py::test_simple::test_case1 passed                                                                                                                                                                                                           [ 33%]
testcase/test_simple.py::test_simple::test_case2 failed                                                                                                                                                                                                           [ 66%]
testcase/test_simple.py::test_simple::test_case3 passed                                                                                                                                                                                                           [100%]

=============================================================================================================================== failures ===============================================================================================================================
________________________________________________________________________________________________________________________ test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.test_simple.test_simple object at 0x000000000382eda0>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testcase2")
        tof = false
>       assert tof
e       assert false

testcase\test_simple.py:18: assertionerror
------------------------------------------------------------------------------------------------------------------------- captured stdout call -------------------------------------------------------------------------------------------------------------------------
testcase2
================================================================================================================== 1 failed, 2 passed in 0.08 seconds ==================================================================================================================

e:\pyspace\testsimple>

如上图会把详细信息都打印出来

指定组别

如果用例中包含多个分组,想要只运行其中一个组,则使用-m "组名"的方式,依然使用如上代码,运行命令和结果如下:

e:\pyspace\testsimple>pytest -s -m "normal"
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 2 deselected / 1 selected                                                                                                                                                                                                                           

testcase\test_simple.py 用于normal组
testcase2
f

=============================================================================================================================== failures ===============================================================================================================================
________________________________________________________________________________________________________________________ test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.test_simple.test_simple object at 0x00000000036d27f0>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testcase2")
        tof = false
>       assert tof
e       assert false

testcase\test_simple.py:18: assertionerror
================================================================================================================ 1 failed, 2 deselected in 0.07 seconds ================================================================================================================

e:\pyspace\testsimple>

使用表达式指定某些用例-k

-k选项允许我们设置表达式来运行某些用例,如下传参就只运行了test_case1和test_case2

e:\pyspace\testsimple>pytest -v -k "case1 or case2"
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'python': '3.7.1', 'platform': 'windows-7-6.1.7601-sp1', 'packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'java_home': 'd:\\project\\jdk1.8'}
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 1 deselected / 2 selected                                                                                                                                                                                                                           

testcase/test_simple.py::test_simple::test_case1 passed                                                                                                                                                                                                           [ 50%]
testcase/test_simple.py::test_simple::test_case2 failed                                                                                                                                                                                                           [100%]

表达式的写法有许多,可以用全称如test_case1这样也可以去掉test_,除了or外也可以使用not来指定那些用例不跑;

遇到失败即停止运行-x

pytest的原本运行规则是每条用例均执行,不管是否有失败,如果我们想在用例运行时遇到失败即停止,则可以使用-x,如下所示,第二条用例失败后则不再运行第三条用例:

e:\pyspace\testsimple>pytest -v -x
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'python': '3.7.1', 'platform': 'windows-7-6.1.7601-sp1', 'packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'java_home': 'd:\\project\\jdk1.8'}
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items                                                                                                                                                                                                                                                       

testcase/test_simple.py::test_simple::test_case1 passed                                                                                                                                                                                                           [ 33%]
testcase/test_simple.py::test_simple::test_case2 failed                                                                                                                                                                                                           [ 66%]

=============================================================================================================================== failures ===============================================================================================================================
________________________________________________________________________________________________________________________ test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.test_simple.test_simple object at 0x00000000037a9b00>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testcase2")
        tof = false
>       assert tof
e       assert false

testcase\test_simple.py:18: assertionerror
------------------------------------------------------------------------------------------------------------------------- captured stdout call -------------------------------------------------------------------------------------------------------------------------
testcase2
================================================================================================================== 1 failed, 1 passed in 0.08 seconds ==================================================================================================================

e:\pyspace\testsimple>

指定运行某个测试py文件

指定运行某个py文件,只需要接上文件相对路径即可:

e:\pyspace\testsimple>pytest -v testcase/test_example.py
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'python': '3.7.1', 'platform': 'windows-7-6.1.7601-sp1', 'packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'java_home': 'd:\\project\\jdk1.8'}
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item                                                                                                                                                                                                                                                        

testcase/test_example.py::test_example::test_aaa passed                                                                                                                                                                                                           [100%]

======================================================================================================================= 1 passed in 0.02 seconds =======================================================================================================================

e:\pyspace\testsimple>

指定运行某个class

写法为:py文件路径::class名称,范例如下:

e:\pyspace\testsimple>pytest -v testcase/test_example.py::test_example2
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'python': '3.7.1', 'platform': 'windows-7-6.1.7601-sp1', 'packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'java_home': 'd:\\project\\jdk1.8'}
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item                                                                                                                                                                                                                                                        

testcase/test_example.py::test_example2::test_bbb passed                                                                                                                                                                                                          [100%]

======================================================================================================================= 1 passed in 0.08 seconds =======================================================================================================================

e:\pyspace\testsimple>

指定运行某个方法:

写法为:py文件路径::class名称::method名称,范例如下:

e:\pyspace\testsimple>pytest -v testcase/test_example.py::test_example2
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'python': '3.7.1', 'platform': 'windows-7-6.1.7601-sp1', 'packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'java_home': 'd:\\project\\jdk1.8'}
rootdir: e:\pyspace\testsimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item                                                                                                                                                                                                                                                        

testcase/test_example.py::test_example2::test_bbb passed                                                                                                                                                                                                          [100%]

======================================================================================================================= 1 passed in 0.08 seconds =======================================================================================================================

e:\pyspace\testsimple>

如上几种也可以组合使用;

其他

pytest还包含许多其他用法,具体用法可以使用pytest --help来查看,如下:

Python教程之pytest命令行方式运行用例

总结

到此这篇关于python教程之pytest命令行方式运行的文章就介绍到这了,更多相关pytest命令行方式运行内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!