为什么我的鼻子测试在使用继承时以 A、AB、ABC 方式重复测试
Why do my nosetests repeat tests in an A, AB, ABC fashion when using inheritance
所以我的 nosetests 项目有问题,所以我隔离了这个问题并试图解决它,但我留下了一些奇怪的行为。请参阅下面我的脚本以供参考。
from unittest import TestCase
class BaseClass(TestCase):
@classmethod
def setup_class(cls):
print("defining base class stuff")
def assert_something(self, expected, actual):
assert expected == actual, "failed test"
class FirstClass(BaseClass):
@classmethod
def setup_class(cls):
super(FirstClass, cls).setup_class()
print("defining first class stuff")
def test_first_class(self):
self.assert_something(1, 1)
class SecondClass(FirstClass):
@classmethod
def setup_class(cls):
super(SecondClass, cls).setup_class()
print("defining second class stuff")
def test_second_class(self):
self.assert_something(5, 4)
class ThirdClass(SecondClass):
@classmethod
def setup_class(cls):
SecondClass.setup_class()
print("defining third class stuff")
def test_third_class(self):
self.assert_something(13, 13)
现在如果我 运行 上面的脚本。我在终端中得到以下输出:
(sandbox_venv) michael@Puter:~/workspace/sandbox$ nosetests -v
test_first_class (test_.FirstClass) ... ok
test_first_class (test_.SecondClass) ... ok
test_second_class (test_.SecondClass) ... FAIL
test_first_class (test_.TestThirdClass) ... ok
test_second_class (test_.TestThirdClass) ... FAIL
test_third_class (test_.TestThirdClass) ... ok
由于我的继承方式。很明显,他们也在继承测试用例并再次 运行ning 它们。这就是我标题中'A, AB, ABC'的意思。
这不是所需的功能。我试图制作这个项目,以便我可以 运行 按特定顺序进行测试,但我也可以使每个测试彼此独立。这样,如果我只进行 运行 ThirdClass 测试,那么它将 运行 每个后续 class 中的 setup_class() 而不是测试。
@Ulrich Eckhardt 感谢您的评论。从那以后,我进一步研究了这一点,发现无法使用继承获得所需的功能。
相反,我删除了继承,而只是从每个 类 设置方法中调用 base_class 中的 setup_class 方法。像这样:
from unittest import TestCase
class BaseClass(TestCase):
@classmethod
def setup_class(cls):
print("defining base class stuff")
@classmethod
def assert_something(cls, expected, actual):
assert expected == actual, "failed test"
class FirstClass():
@classmethod
def setup_class(cls):
BaseClass.setup_class()
print("defining first class stuff")
def test_first_class(self):
BaseClass.assert_something(1, 1)
class SecondClass():
@classmethod
def setup_class(cls):
BaseClass.setup_class()
print("defining second class stuff")
def test_second_class(self):
BaseClass.assert_something(5, 4)
class ThirdClass():
@classmethod
def setup_class(cls):
BaseClass.setup_class()
print("defining third class stuff")
def test_third_class(self):
BaseClass.assert_something(13, 13)
所以我的 nosetests 项目有问题,所以我隔离了这个问题并试图解决它,但我留下了一些奇怪的行为。请参阅下面我的脚本以供参考。
from unittest import TestCase
class BaseClass(TestCase):
@classmethod
def setup_class(cls):
print("defining base class stuff")
def assert_something(self, expected, actual):
assert expected == actual, "failed test"
class FirstClass(BaseClass):
@classmethod
def setup_class(cls):
super(FirstClass, cls).setup_class()
print("defining first class stuff")
def test_first_class(self):
self.assert_something(1, 1)
class SecondClass(FirstClass):
@classmethod
def setup_class(cls):
super(SecondClass, cls).setup_class()
print("defining second class stuff")
def test_second_class(self):
self.assert_something(5, 4)
class ThirdClass(SecondClass):
@classmethod
def setup_class(cls):
SecondClass.setup_class()
print("defining third class stuff")
def test_third_class(self):
self.assert_something(13, 13)
现在如果我 运行 上面的脚本。我在终端中得到以下输出:
(sandbox_venv) michael@Puter:~/workspace/sandbox$ nosetests -v
test_first_class (test_.FirstClass) ... ok
test_first_class (test_.SecondClass) ... ok
test_second_class (test_.SecondClass) ... FAIL
test_first_class (test_.TestThirdClass) ... ok
test_second_class (test_.TestThirdClass) ... FAIL
test_third_class (test_.TestThirdClass) ... ok
由于我的继承方式。很明显,他们也在继承测试用例并再次 运行ning 它们。这就是我标题中'A, AB, ABC'的意思。
这不是所需的功能。我试图制作这个项目,以便我可以 运行 按特定顺序进行测试,但我也可以使每个测试彼此独立。这样,如果我只进行 运行 ThirdClass 测试,那么它将 运行 每个后续 class 中的 setup_class() 而不是测试。
@Ulrich Eckhardt 感谢您的评论。从那以后,我进一步研究了这一点,发现无法使用继承获得所需的功能。
相反,我删除了继承,而只是从每个 类 设置方法中调用 base_class 中的 setup_class 方法。像这样:
from unittest import TestCase
class BaseClass(TestCase):
@classmethod
def setup_class(cls):
print("defining base class stuff")
@classmethod
def assert_something(cls, expected, actual):
assert expected == actual, "failed test"
class FirstClass():
@classmethod
def setup_class(cls):
BaseClass.setup_class()
print("defining first class stuff")
def test_first_class(self):
BaseClass.assert_something(1, 1)
class SecondClass():
@classmethod
def setup_class(cls):
BaseClass.setup_class()
print("defining second class stuff")
def test_second_class(self):
BaseClass.assert_something(5, 4)
class ThirdClass():
@classmethod
def setup_class(cls):
BaseClass.setup_class()
print("defining third class stuff")
def test_third_class(self):
BaseClass.assert_something(13, 13)