我可以用 Nose 嵌套测试用例吗?

发布于 2024-12-25 07:29:42 字数 787 浏览 0 评论 0原文

我已经成为 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 技术交流群。

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

发布评论

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

评论(2

仲春光 2025-01-01 07:29:42

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

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 和tearDown 的纯父测试类。类设置/类TearDown。

这应该允许您任意级别的嵌套,尽管一旦您这样做,您将需要单元测试来进行单元测试......

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.

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

太阳哥哥 2025-01-01 07:29:42

据我所知,还没有,但您可以使用 模块和包级别

那么你的例子将变成:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文