简体   繁体   English

我可以将TestCases与鼻子套在一起吗?

[英]Can I nest TestCases with Nose?

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. 我已经成为RSpec和Jasmine之类的嵌套测试用例上下文的忠实拥护者,并且我想知道是否有任何Nose插件实现了可将类嵌套为上下文的测试查找器。 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. 在上述假设情况下,将在每次测试之前调用system_state.initialize() 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. 我知道有PyVows可以做这样的事情,而且看起来不错,但是我正在寻找可以插入当前项目的东西,该项目已经有许多单元测试/鼻子式测试。

It sounds like you want some of your test to inherit setup code from other tests: 听起来您想让某些测试从其他测试中继承安装代码:

from nose.tools import *
from mysystem import system_state

class TestMySystem (TestCase):
    def setUp(self):
        system_state.initialize()

class WhenItIsSetTo1 (TestMySystem):
    def setUp(self):
        super(WhenItIsSetTo1, self).setUp()
        system_state.set_to(1)

    def test_system_should_be_1 (self):
        assert_equal(system_state.value(), 1)

class WhenItIsSetTo2 (TestMySystem):
    def setUp(self):
        super(WhenItIsSetTo2, self).setUp()
        system_state.set_to(2)

    def test_system_should_be_2 (self):
        assert_equal(system_state.value(), 2)

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. 当我这样做时,我喜欢制作仅提供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: 您的示例将变为:

def setup():
    system_state.initialize()

def teardown():
    system_state.teardown()

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM