基于约束的 Python 单元测试框架

发布于 2024-12-08 01:48:56 字数 400 浏览 0 评论 0原文

CPython 是否有基于约束的单元测试框架(很像我心爱的 NUnit),还是我必须自己推出?

编辑:

这就是我所说的基于约束的意思: http://www.nunit.org/index.php?p=constraintModel& ;r=2.6

即而不是:

assert(z.Count > 5)

它更像是:

assert.that(z.Count, Is.GreaterThan(5))

Is there a constraint-based unit testing framework for CPython (much like my beloved NUnit) or will I have to roll my own?

EDIT:

This is what I mean by constraint-based:
http://www.nunit.org/index.php?p=constraintModel&r=2.6

i.e. instead of:

assert(z.Count > 5)

it's more like:

assert.that(z.Count, Is.GreaterThan(5))

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

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

发布评论

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

评论(4

祁梦 2024-12-15 01:48:56

我不知道 Python 单元测试工具是否存在这样的问题,但如果您必须使用 Python 的功能来自行开发,那么这似乎可以很容易地解决。例如,让我们有一个名为assertThat的函数,如下所示:

def assertThat(val, constraint):
    assert(constraint(val))

约束可以这样定义:

def equal(testVal):
    def c(val):
        return testVal == val

然后,您可以像这样使用它:

assertThat(myVal, equal(5))

它会执行您期望它执行的操作。

这只是一切的开始;我怀疑,通过装饰器、操作符模块和一个下午的休息,您可以创建一套非常全面的非常有用的约束函数!

I don't know if such a thing exists for Python unit testing tools, but it seems like something that can be solved quite easily if you have to roll your own, using Python's functional capabilities. For example, let's have a function called assertThat as follows:

def assertThat(val, constraint):
    assert(constraint(val))

Constraint could be defined this way:

def equal(testVal):
    def c(val):
        return testVal == val

Then, you can just use it like this:

assertThat(myVal, equal(5))

which does what you expect it to do.

That's just the start of it; I suspect that with decorators, the operator module and an afternoon off, you could create a pretty comprehensive set of very useful constraint functions!

意中人 2024-12-15 01:48:56

内置库 unittest 提供了您寻求的功能。您会发现断言方法列表对于做出特定断言非常有用。

from unittest import TestCase

class SomeTest(TestCase):
    def setUp(self):
        self.value = 5

    def testMyValue(self):
        self.assertEqual(self.value, 5)

这与 NUnit 中的约束有些不同,但很容易适应并且非常强大。

The built-in library unittest provides the functionality you seek. You will find the list of assert methods very useful for making specific assertions.

from unittest import TestCase

class SomeTest(TestCase):
    def setUp(self):
        self.value = 5

    def testMyValue(self):
        self.assertEqual(self.value, 5)

This is somewhat different to the constraints in NUnit but it is easy to adapt an can be quite powerful.

无悔心 2024-12-15 01:48:56

回答我自己的问题:没有。真的,没有任何意义。它在 C#/Java 等语言中很有用,但在 Python 中则不然,您可以在 Python 中执行诸如 assert(i in [1, 2, 3]) 之类的操作。

To answer my own question: there is none. And really, there's no point. It's useful in languages like C#/Java, but not in Python where you can do things like assert(i in [1, 2, 3]).

夏花。依旧 2024-12-15 01:48:56

您可能想查看 testtools,它似乎具有您想要的语法。它使测试更具可读性,并且适用于 Python 2.6+

class TestSillySquareServer(TestCase):

    def test_server_is_cool(self):
        self.assertThat(self.server.temperature, Equals("cool"))

    def test_square(self):
        self.assertThat(self.server.silly_square_of(7), Equals(49))

You might like to check out testtools, which appears to have the syntax you are after. It makes the tests more readable, and works in Python 2.6+

class TestSillySquareServer(TestCase):

    def test_server_is_cool(self):
        self.assertThat(self.server.temperature, Equals("cool"))

    def test_square(self):
        self.assertThat(self.server.silly_square_of(7), Equals(49))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文