我可以将TestCases与鼻子套在一起吗? [英] Can I nest TestCases with Nose?

查看:77
本文介绍了我可以将TestCases与鼻子套在一起吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成为RSpec和Jasmine之类的嵌套测试用例上下文的支持者,并且我想知道是否有任何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. 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)

在上述假设情况下,system_state.initialize()将在每次测试之前调用.我知道有PyVows可以做这样的事情,而且看起来不错,但是我正在寻找可以插入当前项目的东西,该项目已经有许多unittest/nose风格的测试.

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.

推荐答案

听起来您希望某些测试从其他测试继承设置代码:

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)

执行此操作时要小心;如果您在父类中具有实际的测试方法,则在子级运行时也将执行它们(当然).当我这样做时,我喜欢制作仅提供setUp,tearDown&的纯父测试类. classSetup/classTearDown.

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...

这篇关于我可以将TestCases与鼻子套在一起吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆