pytest的一些优点是
步骤1)您可以通过安装pytest
pip install pytest==2.9.1
安装完成后,您可以通过
py.test -h
这将显示帮助
创建一个文件夹study_pytest。我们将在此文件夹中创建测试文件。
请在命令行中导航到该文件夹。
在文件夹内创建一个名为test_sample1.py的文件
将以下代码添加到其中并保存
import pytest
def test_file1_method1():
x=5
y=6
assert x+1 == y,"test failed"
assert x == y,"test failed"
def test_file1_method2():
x=5
y=6
assert x+1 == y,"test failed"
使用以下命令运行测试
py.test
您将获得输出为
test_sample1.py F.
============================================== FAILURES ========================================
____________________________________________ test_sample1 ______________________________________
def test_file1_method1():
x=5
y=6
assert x+1 == y,"test failed"
> assert x == y,"test failed"
E AssertionError: test failed
E assert 5 == 6
test_sample1.py:6: AssertionError
在这里test_sample1.pyF。
F表示失败
点(。)表示成功。
在“失败”部分,您可以查看失败的方法和失败行。x == y表示5 == 6,这是错误的。
断言是返回True或False状态的检查。在pytest中,如果断言在测试方法中失败,则该方法的执行在那里停止。该测试方法中的其余代码不会执行,并且pytest将继续使用下一个测试方法。
例子:
assert“ hello” ==“ Hai”是断言失败。
assert 4 == 4是成功的断言
assert True是成功的断言
assert False是断言失败。
考虑
assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
将此代码放在test_file1_method1()中,而不是声明中
assert x == y,“测试失败”
运行测试会将失败显示为AssertionError:测试失败x = 5 y = 6
默认情况下,仅pytest标识开头的文件名TEST_或结束_test作为测试文件。不过,我们可以明确提及其他文件名(稍后说明)。Pytest要求测试方法名称以“ test ” 开头。即使我们明确要求运行这些方法,所有其他方法名称也将被忽略。
查看有效和无效的pytest文件名的一些示例
test_login.py-有效的
login_test.py-有效的
testlogin.py-无效的
logintest.py-无效的
注意:是的,我们可以明确要求pytest选择testlogin.py和logintest.py
查看有效和无效的pytest测试方法的一些示例
def test_file1_method1():-有效的
def testfile1_method1():-有效的
def file1_method1():-无效
注意:即使我们明确提到file1_method1(),pytest也不会运行此方法。
当前,在文件夹study_pytest中,我们有一个文件test_sample1.py。假设我们有多个文件,例如test_sample2.py和test_sample3.py。要从文件夹和子文件夹中的所有文件运行所有测试,我们只需要运行pytest命令。
py.test
这将运行该文件夹中所有以test_开头的文件名和以_test结尾的文件名以及该文件夹下的子文件夹。
要仅从特定文件运行测试,我们可以使用py.test <filename>
py.test test_sample1.py
有时我们不想运行整个测试套件。Pytest允许我们运行特定的测试。我们可以通过两种方式做到这一点
我们已经有test_sample1.py。创建文件test_sample2.py并将以下代码添加到其中
def test_file2_method1():
x=5
y=6
assert x+1 == y,"test failed"
assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
def test_file2_method2():
x=5
y=6
assert x+1 == y,"test failed"
所以我们目前
test_sample1.py
test_file1_method1()
test_file1_method2()
test_sample2.py
test_file2_method1()
test_file2_method2()
选项1)通过子字符串匹配来运行测试
在这里运行所有名称为method1的测试,我们必须运行
py.test -k method1 -v
-k <expression> is used to represent the substring to match
-v increases the verbosity
因此,运行py.test -k method1 -v将为您提供以下结果
test_sample2.py::test_file2_method1 FAILED
test_sample1.py::test_file1_method1 FAILED
============================================== FAILURES ==============================================
_________________________________________ test_file2_method1 _________________________________________
def test_file2_method1():
x=5
y=6
assert x+1 == y,"test failed"
> assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
E AssertionError: test failed because x=5 y=6
E assert 5 == 6
test_sample2.py:5: AssertionError
_________________________________________ test_file1_method1 _________________________________________
@pytest.mark.only
def test_file1_method1():
x=5
y=6
assert x+1 == y,"test failed"
> assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
E AssertionError: test failed because x=5 y=6
E assert 5 == 6
test_sample1.py:8: AssertionError
================================= 2 tests deselected by '-kmethod1' ==================================
=============================== 2 failed, 2 deselected in 0.02 seconds ===============================
在这里,您可以看到最后由'-kmethod1'取消选择的2个测试,分别是test_file1_method2和test_file2_method2
尝试使用各种组合运行,例如:
py.test -k method -v - will run all the four methods
py.test -k methods -v – will not run any test as there is no test name matches the substring 'methods'
选项2)通过标记运行测试
Pytest允许我们使用pytest标记@ pytest.mark为测试方法设置各种属性。要在测试文件中使用标记,我们需要在测试文件上导入pytest。
在这里,我们将不同的标记名称应用于测试方法,并根据标记名称运行特定的测试。我们可以使用定义每个测试名称上的标记
@pytest.mark.<name>.
我们在测试方法上定义了标记set1和set2,我们将使用标记名称来运行测试。使用以下代码更新测试文件
test_sample1.py
import pytest
@pytest.mark.set1
def test_file1_method1():
x=5
y=6
assert x+1 == y,"test failed"
assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
@pytest.mark.set2
def test_file1_method2():
x=5
y=6
assert x+1 == y,"test failed"
test_sample2.py
import pytest
@pytest.mark.set1
def test_file2_method1():
x=5
y=6
assert x+1 == y,"test failed"
assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
@pytest.mark.set1
def test_file2_method2():
x=5
y=6
assert x+1 == y,"test failed"
我们可以通过以下方式运行标记的测试
py.test -m <name>
-m <name> mentions the marker name
运行py.test -m set1,这将运行方法test_file1_method1,test_file2_method1,test_file2_method2。
运行py.test -m set2将运行test_file1_method2。
通常,一个测试套件将具有多个测试文件和数百种测试方法,这将花费大量时间来执行。Pytest允许我们并行运行测试。
为此,我们需要先通过运行pytest-xdist来安装
pip install pytest-xdist
您现在可以通过以下方式运行测试
py.test -n 4
-n <num>通过使用多个工作程序来运行测试。在上面的命令中,将有4位工作人员运行测试。