如何在 pytest 中测试类层次结构?

2024-04-30

我已经使用 pytest 一段时间了,并学会了喜欢参数化和固定装置。我第一次想测试一些具有分支继承结构的类。当然,我想为子类重用测试用例。假设我有以下包结构:

mock
├── pkg
│   ├── child.py
│   ├── grandchild.py
│   └── parent.py
└── tests
    ├── test_child.py
    ├── test_grandchild.py
    └── test_parent.py

如中所述这个问题 https://stackoverflow.com/questions/46040478/how-to-test-a-class-inherited-methods-in-pytest我可以使用固定装置来提供正在测试的类的实例。但是,当我从另一个测试模块中的一个测试模块导入测试类时,(a)感觉这不是 pytest 方式,(b)pytest 将运行导入类的所有测试方法,并作为继承的测试类的一部分再次运行它们。比如说文件test_child.py包含以下内容:

from test_parent import TestParent


class TestChild(TestParent):

    def test_foo(self):
        pass

    def test_bar(self):
        pass

这会导致 pytest 运行测试方法TestParent一次(由于在模块中导入)加上另一次作为TestChild(由于其方法被继承)。

所以我看到两种方法:(1)不要继承基测试类,而只是创建一个固定装置,以便当前实例同时用于TestParent and TestChild, 本质上:

import pytest

from pkg.child import Child
from test_parent import TestParent


@pytest.fixture(scope="class")
def instance():
    return Child()


class TestChild(object):

    def test_foo(self, instance):
        pass

    def test_bar(self, instance):
        pass

(2) 我看到的另一种方法是不导入任何测试类,而只是创建一个参数化的装置test_parent.py这会将所有相关类的实例插入到这些测试方法中。就像是:

import pytest

from pkg.parent import Parent
from pkg.child import Child
from pkg.grandchild import GrandChild


@pytest.fixture(scope="class", params=[Parent, Child, GrandChild])
def instance(request):
    return request.param()


class TestParent(object):

    def test_base(self, instance):
        pass

考虑一下,我更喜欢选项(2),因为它避免了导入,我什至可以完全跳过测试课程。但还有更好的方法吗?


您最初的建议是class TestChild(TestParent):应该可以正常工作。只是避免使用 pytest 收集的名称导入它。例如。:

# test_parent.py
class TestParent:

    def test_parent(self):
        assert True

and

# test_child.py
import test_parent as parent  # renaming not required

class TestChild(parent.TestParent):

    def test_child(self):
        assert True

运行这个:

> pytest -v
======================== test session starts =========================
platform linux -- Python 3.6.5rc1, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 -- /home/flub/.virtualenvs/a713d56197da3b03/bin/python3
cachedir: .pytest_cache
rootdir: /tmp/sandbox, inifile:
collected 3 items                                                                                                                                                                                    

test_child.py::TestChild::test_child PASSED                     [ 33%]
test_child.py::TestChild::test_parent <- test_parent.py PASSED  [ 66%]
test_parent.py::TestParent::test_parent PASSED                  [100%]

=================== 3 passed in 0.01 seconds =========================

请注意,不需要重命名,因为test_仅当函数或方法被视为测试时。但为了避免混乱,这样做是很好的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 pytest 中测试类层次结构? 的相关文章

随机推荐

  • RemoteViews 支持的方法

    我正在尝试将值转发到 Android RemoteView 某些值可以通过使用反射的 set 方法转发 例如背景颜色的工作原理 rv setInt R id viewId setBackgroundColor 0xffff0000 尽管该机
  • 如何在 JavaScript 中将随机对象文本插入到 DOM 中?

    我正在为表单输入字段制作自定义类包装器 这些字段内部包含 DOM 节点 并通过额外的功能方法进行增强 我的问题是是否有与 toString 类似的方法用于附加到 DOM 因为我想直接将对象插入到 DOM 而不是调用其他方法 换句话说 这是我
  • Django 可重用应用程序配置

    我有一些连接到数据库的 Django 中间件代码 我想将中间件变成可重用的应用程序 app 这样我就可以将其打包以分发到许多其他项目中 而无需复制和粘贴 我不明白可重用应用程序应该在哪里配置自身 由于它是用于重新分发的 所以我无法自己编写中
  • Python:从自定义域发送电子邮件

    我正在尝试从 Python 中的自定义域发送电子邮件 我已经弄清楚如何使用 smtplib 从其他域 例如 gmail com 发送电子邮件 示例代码 https stackoverflow com questions 57842922 c
  • 我们可以从 MFCC 系数中恢复音频吗?

    可以从 MFCC 系数中获取音频信号吗 另外 MFCC 系数是否有一个值范围 如果有的话 是什么 如果没有 如何将其归一化在 0 到 1 之间 我尝试使用以下 MATLAB 代码 http labrosa ee columbia edu m
  • 如何在代码中使用毕加索设置背景图像

    我知道毕加索将图像加载到 imageview 等中 但如何使用毕加索设置布局背景图像 My code public class MainActivity extends ActionBarActivity Override protecte
  • 在 dplyr::filter 中传递字符串作为变量名

    我使用 mtcars 数据集来说明我的问题 例如 我想将数据子集到 4 缸汽车 我可以这样做 mtcars gt filter cyl 4 在我的工作中 我需要传递一个字符串变量作为我的列名 例如 var lt cyl mtcars gt
  • 在 C# 中将字符串转换为类型[重复]

    这个问题在这里已经有答案了 如果我收到一个包含类名称的字符串 并且我想将该字符串转换为真实类型 字符串中的类型 我该怎么做 I tried Type GetType System Int32 例如 它似乎有效 但是当我尝试使用自己的对象时
  • 在 XCode 中静态链接 OpenSSL

    我正在尝试链接libssl a and libcrypto aXCode 命令行项目中的静态库 在 Link Binary With Libraries 下 我已在搜索路径中包含 Openssl 头文件 编译成功但执行失败dyld Libr
  • 从滑块输入数据以更改标记颜色

    感谢对此任务的任何帮助 我试图让这个破折号应用程序采用滑块输入值来通过函数更改变量 然后仅更改标记颜色变量 该代码是用 python 编写的 并使用了plotly dash 和plotly 以及pandas numpy 和mapbox 代码
  • 如何防止用户杀死C#应用程序[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 如果您使用 ESET Smart S
  • 我可以访问 TBits 内部位图吗?

    In particular i want to preset desired size fetch a bitmap from external source and then work with data in classy object
  • 使用 UDF 添加文件读取添加到 Hive 资源的文件

    我想知道如何读取使用添加的 Hive 资源ADD FILE来自乌德夫 例如 Hive gt add file users temp key jks Java中的UDF可以读取这个文件吗 在 Udf 中获取此文件的路径是什么 谢谢 大卫 一旦
  • JPEG 颜色在不同浏览器中呈现不一致

    我正在使用的徽标在不同浏览器中的呈现方式有所不同 具体图片可查here https pbs twimg com profile images 741262755236356096 BqpBjB8R jpg and on 这个推特页面 htt
  • RecyclerView 中的单选

    我知道没有默认的选择方法RecyclerView类 但我尝试过以下方式 public void onBindViewHolder ViewHolder holder final int position holder mTextView s
  • 为 SharePoint 2010 Web 部件创建图表

    我已创建从 Web 部件到 Oracle 数据库的连接 其中包含日期和数字 我想在图表中显示这些数据 我在互联网上查找过任何示例 但我很困惑 任何人都可以向我指出一些简单的说明 详细说明如何在 C Sharp 中创建可在 Web 部件上使用
  • 在 ASP.NET MVC 中返回不同的视图同一控制器

    我想根据以下值将用户发送到两个不同页面之一isCustomerEligible 当该变量的值设置为 false 时 它 会调用 Index 但随后返回视图Customer而不是视图Index public ViewResult Index
  • textarea根据内容js或jquery设置高度

    这是我的代码 保持简单
  • 具有单变量优化的 NLopt

    任何人都知道 NLopt 是否适用于单变量优化 尝试运行以下代码 using NLopt function myfunc x grad x 2 end opt Opt LD MMA 1 min objective opt myfunc mi
  • 如何在 pytest 中测试类层次结构?

    我已经使用 pytest 一段时间了 并学会了喜欢参数化和固定装置 我第一次想测试一些具有分支继承结构的类 当然 我想为子类重用测试用例 假设我有以下包结构 mock pkg child py grandchild py parent py