logo

使用pytest执行测试用例:通过、失败以及查看执行详情

作者:很菜不狗2024.01.17 23:58浏览量:46

简介:介绍如何使用pytest执行测试用例,包括通过的测试用例、失败的测试用例以及如何查看测试用例的执行详情。

pytest是一个流行的Python测试框架,它提供了许多有用的功能来帮助开发人员编写和执行测试用例。下面我们将介绍如何使用pytest执行测试用例,包括通过的测试用例、失败的测试用例以及如何查看测试用例的执行详情。
首先,确保已经安装了pytest。如果尚未安装,可以使用以下命令进行安装:

  1. pip install pytest

接下来,创建一个简单的Python模块,其中包含一些函数和测试用例。例如,我们可以创建一个名为example.py的文件,其中包含以下内容:

  1. def add(x, y):
  2. return x + y
  3. def test_add():
  4. assert add(1, 2) == 3

现在,我们可以使用pytest来执行这个测试用例。在终端中,导航到包含example.py文件的目录,并运行以下命令:

  1. pytest example.py

pytest将执行测试用例并输出结果。在这个例子中,测试用例test_add应该通过,输出如下:

  1. ============================= test session starts =============================
  2. platform linux -- Python 3.8.5, pytest-6.2.2, py-1.9.0, pluggy-0.13.1 -- /usr/bin/python3
  3. cachedir: .pytest_cache
  4. rootdir: /home/user/example_project, inifile:
  5. plugins: xdist-1.34.0, cov-2.8.1
  6. collecting ... collected 1 item
  7. test_example.py . [100%]
  8. =========================== 1 passed in 0.01s ===========================

在这个输出中,可以看到测试用例test_add通过了,并且执行时间非常短。如果测试用例失败了,pytest将输出相应的错误信息。例如,如果我们修改测试用例以检查一个永远为False的条件,如下所示:

  1. def test_add():
  2. assert add(1, 2) == 42

然后再次运行pytest命令,输出将如下所示:
``makefile ============================= test session starts ============================= platform linux -- Python 3.8.5, pytest-6.2.2, py-1.9.0, pluggy-0.13.1 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /home/user/example_project, inifile: plugins: xdist-1.34.0, cov-2.8.1 collecting ... collected 1 item test_example.py F [100%] =========================== FAILURES: 1 =========================== ___________________________________________________________________________________ test_add _____________________________________________________________________________________, line 3 assert add(1, 2) == 42 (test_example.py) EXPECTED :42 ACTUAL :3 DIFF :42 != 3 (49) --------------------------------------------------------------------------- Captured stdout call--------------------------------------------------------------------------- AssertionError: assert add(1, 2) == 42 (test_example.py), line 3 EXPECTED :42 ACTUAL :3 DIFF :42 != 3 (49) tests/test_example.py: line 6: * AssertionError: assert add(1, 2) == 42* ============================ short test summary info =========================== test_example.py: test_add (line: 3) failed with AssertionError (see above for traceback) ERROR collecting test_example.py (collected 1 items) ERROR collecting tests/test_example.py (collected 1 items) ============================ 0 passed, 1 failed in 0.01s ============================ <!> Remember we are using the xdist plugin to distribute tests, make sure you didn't confuse it by running tests with pytest directly instead of pytest-xdist! <!> Remember to also check the other output captures (stderr, html, json) that pytest generates to get more insights into the error! <!> You can use the following options to get more output or different output formats:-vor—verbose for more detailed output

相关文章推荐

发表评论

活动