我可以用 Nose 嵌套测试用例吗?
我已经成为 RSpec 和 Jasmine 等嵌套测试用例上下文的粉丝,我想知道是否有任何 Nose 插件可以实现测试查找器,允许您将类嵌套为上下文。结果测试如下所示:
from nose.tools import *
from mysystem import system_state
class TestMySystem (TestCase):
def setUp(self):
system_state.initialize()
class WhenItIsSetTo1 (TestCase):
def setUp(self):
system_state.set_to(1)
def test_system_should_be_1 (self):
assert_equal(system_state.value(), 1)
class WhenItIsSetTo2 (TestCase):
def setUp(self):
system_state.set_to(2)
def test_system_should_be_2 (self):
assert_equal(system_state.value(), 2)
在上述假设情况下,system_state.initialize()
将在每次测试之前调用。我知道 PyVows 可以做这样的事情,而且看起来不错,但我正在寻找一些东西可以插入到我当前的项目中,该项目已经有许多单元测试/鼻子式测试。
I've become a fan of nested test case contexts in things like RSpec and Jasmine, and I'm wondering if there are any Nose plugins that implement a test finder that allows you to nest classes as context. The resulting tests would look something like the following:
from nose.tools import *
from mysystem import system_state
class TestMySystem (TestCase):
def setUp(self):
system_state.initialize()
class WhenItIsSetTo1 (TestCase):
def setUp(self):
system_state.set_to(1)
def test_system_should_be_1 (self):
assert_equal(system_state.value(), 1)
class WhenItIsSetTo2 (TestCase):
def setUp(self):
system_state.set_to(2)
def test_system_should_be_2 (self):
assert_equal(system_state.value(), 2)
In the above hypothetical case, system_state.initialize()
will be called before each test. I know there is PyVows for doing something like this, and it looks good, but I'm looking for something to plug in to my current project, which already has a number of unittest-/nose-style tests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您希望某些测试继承其他测试的设置代码:
执行此操作时要小心;如果您在父类中有实际的测试方法,那么它们也会在子类运行时执行(当然)。当我这样做时,我喜欢制作仅提供setUp、tearDown 和tearDown 的纯父测试类。类设置/类TearDown。
这应该允许您任意级别的嵌套,尽管一旦您这样做,您将需要单元测试来进行单元测试......
It sounds like you want some of your test to inherit setup code from other tests:
Be careful when you do this; if you have actual test methods in the parent class, they will also be executed when the child is run (of course). When I do this, I like to make pure parent test classes that only provide setUp, tearDown & classSetup/ classTearDown.
This should allow you an arbitrary level of nesting, though once you do that you're going to need unit tests for your unit tests...
据我所知,还没有,但您可以使用 模块和包级别。
那么你的例子将变成:
Not as far as I know, but you can achieve a similar effect with setup and teardown methods at the module and package levels.
Your example would then become: