前言
用命令行方式调用用例是我们最常用的方式,这方面确实比java的TestNG框架要好用许多,至少不用写xml文件,为了提供定制化运行用例的方式,pytest提供了许多运行命令以供定制化运行某一类测试用例或者某个测试用例等;
pycharm里命令行运行用例
在pycharm里写好了测试用例后如何运行呢?pycharm里好像并没有像eclipse里提供TestNG用的插件一样可以一键执行的方式,那么我们可以使用命令行的方式来进行,如下图所示为一个用例文件:
代码如下:
#-*- 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窗口:
会自动切换到当前工程目录下,而后即可使用pytest的命令了,如下对运行结果简单做下说明:
终端中使用pytest
在终端中使用pytest也是和在pycharm中类似,如下以windows系统为例:
先切换到用例所在工程或者目录而后运行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 ========&