基于约束的 Python 单元测试框架
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道 Python 单元测试工具是否存在这样的问题,但如果您必须使用 Python 的功能来自行开发,那么这似乎可以很容易地解决。例如,让我们有一个名为assertThat的函数,如下所示:
约束可以这样定义:
然后,您可以像这样使用它:
它会执行您期望它执行的操作。
这只是一切的开始;我怀疑,通过装饰器、操作符模块和一个下午的休息,您可以创建一套非常全面的非常有用的约束函数!
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:
Constraint could be defined this way:
Then, you can just use it like this:
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!
内置库 unittest 提供了您寻求的功能。您会发现断言方法列表对于做出特定断言非常有用。
这与 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.
This is somewhat different to the constraints in NUnit but it is easy to adapt an can be quite powerful.
回答我自己的问题:没有。真的,没有任何意义。它在 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]).
您可能想查看 testtools,它似乎具有您想要的语法。它使测试更具可读性,并且适用于 Python 2.6+
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+