PyTest 教程:什么是、如何安装、框架、断言
什么是 PyTest?
py测试 是一个测试框架,允许用户使用编写测试代码 Python 编程语言。它可以帮助您为数据库、API 或 UI 编写简单且可扩展的测试用例。PyTest 主要用于编写 API 测试。它有助于编写从简单的单元测试到复杂的功能测试的测试。
为什么使用 PyTest?
pytest 的一些优点包括
- 由于其语法简单易懂,因此非常容易上手。
- 可以并行运行测试。
- 可以运行特定测试或测试子集
- 自动检测测试
- 跳过测试
- 开源
如何安装 PyTest
以下是如何安装PyTest的过程:
步骤1) 您可以通过以下方式安装 pytest
pip install pytest==2.9.1
安装完成后,你可以使用以下命令进行确认
py.test -h
这将显示帮助
第一个基本 PyTest
现在,我们将通过一个基本的 PyTest 示例学习如何使用 Pytest。
创建一个文件夹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.py F。
F 表示失败
点(.)表示成功。
在失败部分,您可以看到失败的方法和失败行。这里 x==y 表示 5==6,即错误。
接下来在本 PyTest 教程中,我们将学习 PyTest 中的断言。
PyTest 中的断言
Pytest 断言是返回 True 或 False 状态的检查。在 Python Pytest,如果断言在测试方法中失败,则该方法的执行将就此停止。该测试方法中的其余代码将不会执行,并且 Pytest 断言将继续执行下一个测试方法。
Pytest 断言示例:
assert "hello" == "Hai" is an assertion failure. assert 4==4 is a successful assertion assert True is a successful assertion assert False is an assertion failure.
考虑
assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
将此代码放在 test_file1_method1() 中,而不是断言中
assert x == y,"test failed"
运行测试将失败,如下所示 AssertionError:测试失败 x=5 y=6
PyTest 如何识别测试文件和测试方法
默认情况下,pytest 仅识别以以下名称开头的文件名 测试_ 或以 _测试 作为测试文件。不过,我们可以明确提及其他文件名(稍后解释)。Pytest 要求测试方法名称以 “测试。”即使我们明确要求运行这些方法,所有其他方法名称也将被忽略。
查看有效和无效的 pytest 文件名的一些示例
test_login.py - valid login_test.py - valid testlogin.py -invalid logintest.py -invalid
注意:是的,我们可以明确要求 pytest 选择 testlogin.py 和 logintest.py
查看有效和无效的 pytest 测试方法的一些示例
def test_file1_method1(): - valid def testfile1_method1(): - valid def file1_method1(): - invalid
注意:即使我们明确提到 file1_method1() pytest 也不会运行此方法。
从特定文件和多个文件运行多个测试
目前,在文件夹study_pytest中,我们有一个文件test_sample1.py。假设我们有多个文件,比如test_sample2.py、test_sample3.py。要从文件夹和子文件夹中的所有文件运行所有测试,我们只需运行pytest命令。
py.test
这将运行该文件夹及其下子文件夹中所有以 test_ 开头的文件和以 _test 结尾的文件。
要仅从特定文件运行测试,我们可以使用 py.test
py.test test_sample1.py
使用 PyTest 运行整个测试的子集
有时我们不想运行整个测试套件。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 ===============================
在这里你可以看到结尾 通过“-kmethod2”取消选择 1 个测试 分别是 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,并将使用标记名称运行测试。使用以下代码更新测试文件
测试样本1.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"
测试样本2.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 允许我们并行运行测试。
为此,我们需要首先通过运行安装 pytest-xdist
pip install pytest-xdist
您现在可以运行测试
py.test -n 4
-n使用多个 worker 运行测试。在上面的命令中,将有 4 个 worker 运行测试。
Pytest Fixtures
当我们想在每个测试方法之前运行一些代码时,就会使用 Fixture。因此,我们定义 Fixture,而不是在每个测试中重复相同的代码。通常,Fixture 用于初始化数据库连接、传递基数等
通过将方法标记为
@pytest.fixture
测试方法可以通过将装置作为输入参数来使用 Pytest 装置。
创建新文件 test_basic_fixture.py,代码如下
import pytest @pytest.fixture def supply_AA_BB_CC(): aa=25 bb =35 cc=45 return [aa,bb,cc] def test_comparewithAA(supply_AA_BB_CC): zz=35 assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed" def test_comparewithBB(supply_AA_BB_CC): zz=35 assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed" def test_comparewithCC(supply_AA_BB_CC): zz=35 assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"
服务
- 我们有一个名为 supply_AA_BB_CC 的装置。此方法将返回 3 个值的列表。
- 我们有 3 种测试方法来比较每个值。
每个测试函数都有一个输入参数,其名称与可用的 Fixture 匹配。然后,Pytest 调用相应的 Fixture 方法,返回的值将存储在输入参数中,这里是列表 [25,35,45]。现在,列表项正在测试方法中用于比较。
现在运行测试并查看结果
py.test test_basic_fixture
test_basic_fixture.py::test_comparewithAA FAILED test_basic_fixture.py::test_comparewithBB PASSED test_basic_fixture.py::test_comparewithCC FAILED ============================================== FAILURES ============================================== _________________________________________ test_comparewithAA _________________________________________ supply_AA_BB_CC = [25, 35, 45] def test_comparewithAA(supply_AA_BB_CC): zz=35 > assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed" E AssertionError: aa and zz comparison failed E assert 25 == 35 test_basic_fixture.py:10: AssertionError _________________________________________ test_comparewithCC _________________________________________ supply_AA_BB_CC = [25, 35, 45] def test_comparewithCC(supply_AA_BB_CC): zz=35 > assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed" E AssertionError: cc and zz comparison failed E assert 45 == 35 test_basic_fixture.py:16: AssertionError ================================= 2 failed, 1 passed in 0.05 seconds =================================
由于zz=BB=35,因此测试test_comparewithBB通过,其余2个测试失败。
Fixture 方法的作用域仅在其定义的测试文件中。如果我们尝试在其他测试文件中访问 Fixture,则会收到错误消息,提示 Fixture 未找到“supply_AA_BB_CC” 用于其他文件中的测试方法。
为了对多个测试文件使用相同的装置,我们将在名为 conftest.py 的文件中创建装置方法。
让我们通过下面的 PyTest 示例来看一下。使用以下代码创建 3 个文件 conftest.py、test_basic_fixture.py、test_basic_fixture2.py
conftest.py
import pytest @pytest.fixture def supply_AA_BB_CC(): aa=25 bb =35 cc=45 return [aa,bb,cc]
测试基本装置.py
import pytest def test_comparewithAA(supply_AA_BB_CC): zz=35 assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed" def test_comparewithBB(supply_AA_BB_CC): zz=35 assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed" def test_comparewithCC(supply_AA_BB_CC): zz=35 assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"
测试基本装置2.py
import pytest def test_comparewithAA_file2(supply_AA_BB_CC): zz=25 assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed" def test_comparewithBB_file2(supply_AA_BB_CC): zz=25 assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed" def test_comparewithCC_file2(supply_AA_BB_CC): zz=25 assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"
pytest 将首先在测试文件中查找 Fixture,如果未找到,它将在 conftest.py 中查找
通过 py.test -k test_comparewith -v 运行测试,得到以下结果
test_basic_fixture.py::test_comparewithAA FAILED test_basic_fixture.py::test_comparewithBB PASSED test_basic_fixture.py::test_comparewithCC FAILED test_basic_fixture2.py::test_comparewithAA_file2 PASSED test_basic_fixture2.py::test_comparewithBB_file2 FAILED test_basic_fixture2.py::test_comparewithCC_file2 FAILED
Pytest 参数化测试
参数化测试的目的是针对多组参数运行测试。我们可以通过@pytest.mark.parametrize 来实现这一点。
我们将通过下面的 PyTest 示例看到这一点。在这里,我们将向测试方法传递 3 个参数。此测试方法将添加前 2 个参数并将其与第 3 个参数进行比较。
使用以下代码创建测试文件 test_addition.py
import pytest @pytest.mark.parametrize("input1, input2, output",[(5,5,10),(3,5,12)]) def test_add(input1, input2, output): assert input1+input2 == output,"failed"
此处的测试方法接受 3 个参数 - 输入 1、输入 2、输出。它将输入 1 和输入 2 相加,并与输出进行比较。
让我们通过 py.test -k test_add -v 运行测试并查看结果
test_addition.py::test_add[5-5-10] PASSED test_addition.py::test_add[3-5-12] FAILED ============================================== FAILURES ============================================== __________________________________________ test_add[3-5-12] __________________________________________ input1 = 3, input2 = 5, output = 12 @pytest.mark.parametrize("input1, input2, output",[(5,5,10),(3,5,12)]) def test_add(input1, input2, output): > assert input1+input2 == output,"failed" E AssertionError: failed E assert (3 + 5) == 12 test_addition.py:5: AssertionError
你可以看到测试运行了 2 次 - 一次检查 5+5 ==10,另一次检查 3+5 ==12
test_addition.py::test_add[5-5-10]已通过
test_addition.py::test_add[3-5-12] 失败
Pytest Xfail/跳过测试
在某些情况下,我们不想执行测试,或者 测试用例 与特定时间无关。在这种情况下,我们可以选择不通过测试或跳过测试
失败的测试将被执行,但它不会被计入部分失败或通过的测试。如果该测试失败,则不会显示任何回溯。我们可以使用以下方法对测试进行失败测试
请参阅pytest.mark.xfail。
跳过测试意味着测试将不会被执行。我们可以使用以下命令跳过测试
跳过。
使用以下代码编辑 test_addition.py
import pytest @pytest.mark.skip def test_add_1(): assert 100+200 == 400,"failed" @pytest.mark.skip def test_add_2(): assert 100+200 == 300,"failed" @pytest.mark.xfail def test_add_3(): assert 15+13 == 28,"failed" @pytest.mark.xfail def test_add_4(): assert 15+13 == 100,"failed" def test_add_5(): assert 3+2 == 5,"failed" def test_add_6(): assert 3+2 == 6,"failed"
服务
- test_add_1 和 test_add_2 被跳过,不会被执行。
- test_add_3 和 test_add_4 已 xfailed。这些测试将被执行,并将成为 xfailed(测试失败时)或 xpassed(测试通过时)测试的一部分。不会有任何失败回溯。
- test_add_5 和 test_add_6 将被执行,并且 test_add_6 将报告失败并进行回溯,而 test_add_5 则通过
通过 py.test test_addition.py -v 执行测试并查看结果
test_addition.py::test_add_1 SKIPPED test_addition.py::test_add_2 SKIPPED test_addition.py::test_add_3 XPASS test_addition.py::test_add_4 xfail test_addition.py::test_add_5 PASSED test_addition.py::test_add_6 FAILED ============================================== FAILURES ============================================== _____________________________________________ test_add_6 _____________________________________________ def test_add_6(): > assert 3+2 == 6,"failed" E AssertionError: failed E assert (3 + 2) == 6 test_addition.py:24: AssertionError ================ 1 failed, 1 passed, 2 skipped, 1 xfailed, 1 xpassed in 0.07 seconds =================
结果 XML
我们可以创建 XML 格式的测试结果,并将其提供给持续集成服务器进行进一步处理。这可以通过以下方式实现:
py.test test_sample1.py -v –junitxml=”结果.xml”
result.xml 将记录测试执行结果。下面是示例 result.xml
<?xml version="1.0" encoding="UTF-8"?> <testsuite errors="0" failures="1" name="pytest" skips="0" tests="2" time="0.046"> <testcase classname="test_sample1" file="test_sample1.py" line="3" name="test_file1_method1" time="0.001384973526"> <failure message="AssertionError:test failed because x=5 y=6 assert 5 ==6"> @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) E AssertionError: test failed because x=5 y=6 E assert 5 == 6 test_sample1.py:9: AssertionError </failure> </testcase> <testcase classname="test_sample1" file="test_sample1.py" line="10" name="test_file1_method2" time="0.000830173492432" /> </testsuite>
从我们可以看到总共两个测试,其中一个失败了。下面你可以看到每个执行测试的详细信息标签。
Pytest 框架测试 API
现在我们将创建一个小型 pytest 框架来测试 API。这里使用的 API 是来自 https://reqres.in/。本网站仅提供可测试的API。本网站不存储我们的数据。
在这里我们将编写一些测试
- 列出一些用户
- 使用用户登录
使用给定的代码创建以下文件
conftest.py – 有一个装置,它将为所有测试方法提供基本 URL
import pytest @pytest.fixture def supply_url(): return "https://reqres.in/api"
test_list_user.py – 包含列出有效和无效用户的测试方法
- test_list_valid_user 测试有效用户获取并验证响应
- test_list_invaliduser 测试无效用户获取并验证响应
import pytest import requests import json @pytest.mark.parametrize("userid, firstname",[(1,"George"),(2,"Janet")]) def test_list_valid_user(supply_url,userid,firstname): url = supply_url + "/users/" + str(userid) resp = requests.get(url) j = json.loads(resp.text) assert resp.status_code == 200, resp.text assert j['data']['id'] == userid, resp.text assert j['data']['first_name'] == firstname, resp.text def test_list_invaliduser(supply_url): url = supply_url + "/users/50" resp = requests.get(url) assert resp.status_code == 404, resp.text
test_login_user.py – 包含测试登录功能的测试方法。
- test_login_valid 测试使用电子邮件和密码的有效登录尝试
- test_login_no_password 测试没有通过密码的无效登录尝试
- test_login_no_email 测试未发送电子邮件的无效登录尝试。
import pytest import requests import json def test_login_valid(supply_url): url = supply_url + "/login/" data = {'email':'test@test.com','password':'something'} resp = requests.post(url, data=data) j = json.loads(resp.text) assert resp.status_code == 200, resp.text assert j['token'] == "QpwL5tke4Pnpja7X", resp.text def test_login_no_password(supply_url): url = supply_url + "/login/" data = {'email':'test@test.com'} resp = requests.post(url, data=data) j = json.loads(resp.text) assert resp.status_code == 400, resp.text assert j['error'] == "Missing password", resp.text def test_login_no_email(supply_url): url = supply_url + "/login/" data = {} resp = requests.post(url, data=data) j = json.loads(resp.text) assert resp.status_code == 400, resp.text assert j['error'] == "Missing email or username", resp.text
使用 py.test -v 运行测试
结果如下
test_list_user.py::test_list_valid_user[1-George] PASSED test_list_user.py::test_list_valid_user[2-Janet] PASSED test_list_user.py::test_list_invaliduser PASSED test_login_user.py::test_login_valid PASSED test_login_user.py::test_login_no_password PASSED test_login_user.py::test_login_no_email PASSED
更新测试并尝试各种输出
总结
在本 PyTest 教程中,我们介绍了
- 使用以下方式安装 pytest 点安装 pytest=2.9.1
- 简单的 pytest 程序并用 py.test 命令运行它。
- 断言语句,assert x==y,将返回 True 或 False。
- pytest 如何识别测试文件和方法。
- 测试文件以 测试_ 或以 _测试
- 测试方法开始于 测试
- py.test 命令将运行该文件夹及其子文件夹中的所有测试文件。要运行特定文件,我们可以使用命令 py.test
- 运行测试方法的子集
- 按子字符串匹配对测试名称进行分组.py.test -k -v 将运行所有具有以其名义。
- 通过标记运行测试。使用@pytest.mark 标记测试。并使用 pytest -m 运行测试运行标记为的测试。
- 并行运行测试
- 使用 pip install pytest-xdist 安装 pytest-xdist
- 使用 py.test -n NUM 运行测试,其中 NUM 是工人的数量
- 通过使用 @pytest.fixture 标记方法,创建 Fixture 方法以在每次测试之前运行代码
- 固定方法的范围是在其定义的文件内。
- 通过在 conftest.py 文件中定义一个装置方法,就可以在多个测试文件中访问它。
- 测试方法可以通过使用 Pytest 装置作为输入参数来访问它。
- 参数化测试以针对多组输入运行。
@pytest.mark.parametrize(“输入1,输入2,输出”,[(5,5,10),(3,5,12)])
def test_add(输入1,输入2,输出):
断言输入1 + 输入2 == 输出,“失败”
将使用输入 (5,5,10) 和 (3,5,12) 运行测试 - 使用@pytets.mark.skip 和@pytest.mark.xfail 跳过/xfail 测试
- 使用 py.test test_sample1.py -v –junitxml=”result.xml” 创建涵盖已执行测试详细信息的 XML 格式的测试结果
- 用于测试 API 的 pytest 框架示例