Python Unittest2 - 避免在 discovery() 中包含测试用例

发布于 2024-11-14 10:45:51 字数 1012 浏览 3 评论 0原文

我在 Python2.5 上使用 unittest2 来发现 unittest.TestLoader.discover 的测试,如下所示:

suite = unittest2.loader.TestLoader().discover(test_path)
unittest2.TextTestRunner(verbosity=2,
        resultclass=ColorTestResult).run(suite)

对于某些 test_path< /code> 在我的项目的基础上。

我有一个基类,它被许多其他基类扩展和重载,但我想测试这些导数是否没有回归。我们将该基类称为 A 及其派生类 A1A2 等。

我想创建一个 unittest2.TestCase< /code> 可以为 A 的每个派生类重载的基类。换句话说,我想要一个像这样的层次结构:

class A:
    pass

class A1(A):
    pass

class UT(unittest2.TestCase):
    target = A

class UT2(UT):
    target = A1

现在的技巧是我将 A 制作成一个抽象类,并且 UT 将失败几乎所有适合 UT2 的测试用例,等等。

对我来说,最简单的解决方案似乎是让 unittest2 的 discover 以某种方式“跳过”UT< /代码>。我认为通过将其放入一个匹配模式“test*.py”以外的文件中是可能的,尽管情况似乎并非如此。

针对上述情况是否有任何可能合适的解决方案?

如果有任何想法和建议,我将不胜感激。

I'm using unittest2 on Python2.5 to discover tests with unittest.TestLoader.discover, like this:

suite = unittest2.loader.TestLoader().discover(test_path)
unittest2.TextTestRunner(verbosity=2,
        resultclass=ColorTestResult).run(suite)

for some test_path at the base of my project.

I've a base class that's extended and overloaded by numerous others, but I'd like to test that those derivatives do not have regressions. Let's call that base class A and its derivates A1, A2, etc.

I'd like to create a unittest2.TestCase base class that can be overloaded for each of the derivatives of A. In other words, I'd like to have a hierarchy something like this:

class A:
    pass

class A1(A):
    pass

class UT(unittest2.TestCase):
    target = A

class UT2(UT):
    target = A1

Now the trick is that I'm making A into an abstract class, and UT will fail on virtually all of the test cases that would appropriately pass for UT2, etc.

The simplest solution to me seems to be to have unittest2's discover somehow "skip" UT. I would think this would be possible by putting it into a file other than one matching patter 'test*.py', though this seems not to be the case.

Are there any solutions to the above scenario that might be appropriate?

I'd be grateful for any thoughts and suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2024-11-21 10:45:51

我猜测问题在于 UT2 继承自 UT,因此定义 UT2 的模块会导入 UT - 从而使其在模块命名空间中可用。因此,即使发现不是查看定义UT的模块,它仍然可以在UT2模块中找到TestCase。

这是使用本身定义测试的基本测试用例的普遍问题。

有几种可能性。一种是通过函数公开 UT,因此包含 UT2 的模块会导入函数而不是直接导入 UT:

class UT2(get_base_class()):
    target = A1

可以使用 super() 访问 UT 上的属性。

另一个也许更好的解决方案是让 UT 更加抽象。您可以省略 UT 上的目标(因为它似乎没有任何作用?),如果没有目标(或者目标是 A),则让 setUp 调用skipTest。

第三种解决方案是让 UT 不继承 TestCase,而是将其用作 mixin:

class UT2(TestCase, UT):
     target = A1

I'm guessing that the problem is that UT2 inherits from UT, so the module where UT2 is defined imports UT - and therefore makes it available in the module namespace. So even though discovery is not looking at the module where UT is defined, it can still find the TestCase in the UT2 module.

This is a general problem with having base TestCases that themselves define tests.

There are a few possibilities. One would be to expose UT via a function, so your module containing UT2 imports the function instead of UT directly:

class UT2(get_base_class()):
    target = A1

Attributes on UT can be accessed using super().

Another, perhaps better, solution would be to make UT more abstract as well. You could omit the target on UT (as it seems to serve no purpose?) and have setUp call skipTest if there is no target (or if the target is A).

A third solution would be to have UT not inherit from TestCase, but use it as a mixin:

class UT2(TestCase, UT):
     target = A1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文