前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >为什么要使用PyTest?

为什么要使用PyTest?

作者头像
用户7466307
发布2020-06-16 17:14:28
1.2K0
发布2020-06-16 17:14:28
举报

pytest的一些优点是

  • 由于语法简单易上手,因此非常容易上手。
  • 可以并行运行测试。
  • 可以运行特定的测试或部分测试
  • 自动检测测试
  • 跳过测试
  • 开源的

如何安装PyTest

步骤1)您可以通过安装pytest

代码语言:javascript
复制
pip install pytest==2.9.1 

安装完成后,您可以通过

代码语言:javascript
复制
py.test -h

这将显示帮助

第一个基本的PyTest

创建一个文件夹study_pytest。我们将在此文件夹中创建测试文件。

请在命令行中导航到该文件夹。

在文件夹内创建一个名为test_sample1.py的文件

将以下代码添加到其中并保存

代码语言:javascript
复制
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" 

使用以下命令运行测试

代码语言:javascript
复制
py.test

您将获得输出为

代码语言:javascript
复制
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,这是错误的。

PyTest中的断言

断言是返回True或False状态的检查。在pytest中,如果断言在测试方法中失败,则该方法的执行在那里停止。该测试方法中的其余代码不会执行,并且pytest将继续使用下一个测试方法。

例子:

代码语言:javascript
复制
assert“ hello” ==“ Hai”是断言失败。
assert 4 == 4是成功的断言
assert True是成功的断言
assert False是断言失败。

考虑

代码语言:javascript
复制
assert x == y,"test failed because x=" + str(x) + " y=" + str(y)

将此代码放在test_file1_method1()中,而不是声明中

代码语言:javascript
复制
assert x == y,“测试失败”

运行测试会将失败显示为AssertionError:测试失败x = 5 y = 6

pytest如何识别测试文件和测试方法

默认情况下,仅pytest标识开头的文件名TEST_或结束_test作为测试文件。不过,我们可以明确提及其他文件名(稍后说明)。Pytest要求测试方法名称以“ test ” 开头。即使我们明确要求运行这些方法,所有其他方法名称也将被忽略。

查看有效和无效的pytest文件名的一些示例

代码语言:javascript
复制
test_login.py-有效的
login_test.py-有效的
testlogin.py-无效的
logintest.py-无效的

注意:是的,我们可以明确要求pytest选择testlogin.py和logintest.py

查看有效和无效的pytest测试方法的一些示例

代码语言:javascript
复制
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命令。

代码语言:javascript
复制
py.test

这将运行该文件夹中所有以test_开头的文件名和以_test结尾的文件名以及该文件夹下的子文件夹。

要仅从特定文件运行测试,我们可以使用py.test <filename>

代码语言:javascript
复制
py.test test_sample1.py

运行整个测试的一部分

有时我们不想运行整个测试套件。Pytest允许我们运行特定的测试。我们可以通过两种方式做到这一点

  • 通过子字符串匹配对测试名称进行分组
  • 按标记分组测试

我们已经有test_sample1.py。创建文件test_sample2.py并将以下代码添加到其中

代码语言:javascript
复制
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"

所以我们目前

代码语言:javascript
复制
test_sample1.py
  test_file1_method1()
  test_file1_method2()

test_sample2.py
  test_file2_method1()
  test_file2_method2()

选项1)通过子字符串匹配来运行测试

在这里运行所有名称为method1的测试,我们必须运行

代码语言:javascript
复制
py.test -k method1 -v
-k <expression> is used to represent the substring to match
-v increases the verbosity

因此,运行py.test -k method1 -v将为您提供以下结果

代码语言:javascript
复制
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

尝试使用各种组合运行,例如:

代码语言:javascript
复制
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。

在这里,我们将不同的标记名称应用于测试方法,并根据标记名称运行特定的测试。我们可以使用定义每个测试名称上的标记

代码语言:javascript
复制
@pytest.mark.<name>.      

我们在测试方法上定义了标记set1和set2,我们将使用标记名称来运行测试。使用以下代码更新测试文件

test_sample1.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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"

我们可以通过以下方式运行标记的测试

代码语言:javascript
复制
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来安装

代码语言:javascript
复制
pip install pytest-xdist

您现在可以通过以下方式运行测试

代码语言:javascript
复制
py.test -n 4

-n <num>通过使用多个工作程序来运行测试。在上面的命令中,将有4位工作人员运行测试。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试test 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 如何安装PyTest
  • 第一个基本的PyTest
  • PyTest中的断言
  • pytest如何识别测试文件和测试方法
  • 从一个或多个文件运行多个测试。
  • 运行整个测试的一部分
  • 并行运行测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档