我正在尝试编写一种测试一些XML文件的方法。XML文件描述了科学分析程序的输入,其中可以定义各种参数。我想为我的XML文件编写单元测试,这样我就知道程序配置正确了。
我目前作为一个库来执行这个操作,其中包含了一个包含各种测试的基测试类,以及一些用于子组件的混合测试。但是这些子组件被重复了很多次,所以我希望对每个混合组件运行一次测试,例如:
class BaseTest(object):
xmlfile = '...'
...
class ComponentMixin(object):
xmlid = None #
var = None #
def test_var(self):
assert self.var == "whatever_the_value_is_in self.xmlfile"
# ... and a number of other tests and variables...。现在,对于每个分析,可以使用不同的参数定义多个组件。我希望能做这样的事--
class MyFirstComponentMixin(ComponentMixin):
xmlid = 'component1'
var = 'one'
class MySecondComponentMixin(ComponentMixin):
xmlid = 'component2'
var = 'two'
class MyTest(BaseTest, MyFirstComponentMixin, MySecondComponentMixin, unittest.TestCase):
xmlfile = '...'..。但问题是,test_var只会被称为component2,而不是component2。有什么办法解决这个问题,还是有更好的解决办法?
发布于 2015-07-21 01:25:42
正如您在评论中所建议的:组合比继承更能解决您的问题。其思想是为XML文件的各个部分定义多个独立的TestCase(部件),然后将它们组合成单个TestSuite (组合)。
图书馆
它是部分的基类。
class BaseTestCase(unittest.TestCase):
xmlfile = None # will be set by containing test suite它是一个抽象的组件测试用例实现。
class ComponentTestCase(BaseTestCase):
xmlid = None
var = None
def test_var(self):
assert self.var == "whatever_the_value_is_in self.xmlfile"这是我们的复合材料的基础。它定义了xmlfile从复合部件到其部件的方便复制。
class BaseTestSuite(unittest.TestSuite):
xmlfile = None
def addTest(self, test):
if isinstance(test, BaseTestCase):
test.xmlfile = self.xmlfile
super(BaseTestSuite, self).addTest(test)使用
它是特定的部分,它测试XML的某些特定方面:
class MySpecificTestCase(BaseTestCase):
def test_something_specific(self):
self.assertEqual(4, 2 + 2)这些部件测试特定组件:
class MyFirstComponentTestCase(ComponentTestCase):
xmlid = 'component1'
var = 'one'
class MySecondComponentTestCase(ComponentTestCase):
xmlid = 'component2'
var = 'two'下面是要测试的XML组合。
class MyTest(BaseTestSuite):
xmlfile = '<some_xml></some_xml>'我们定义测试以返回包含所有TestCase的TestSuite。
def load_tests(loader, standard_tests, pattern):
return MyTest((
loader.loadTestsFromTestCase(MySpecificTestCase),
loader.loadTestsFromTestCase(MyFirstComponentTestCase),
loader.loadTestsFromTestCase(MySecondComponentTestCase)
))这种方法有一个限制:不能从单个Python文件中测试很少的XML文件。基本上可以,但是输出不会帮助您识别哪个XML文件被破坏了。
你的案子有点棘手。unittest是为了测试代码而不是数据而设计的。也许对XML模式的验证是你所需要的。
发布于 2022-08-11 14:58:54
虽然这个问题的题目正是我想要的,但答案并不完全符合我的情况。
也许是因为这个问题是关于数据的测试,而不是代码。
不过,我还是找到了使用多个继承来实现多个混合的这示例(代码复制-粘贴在下面)。
不过,在遵循此模式之前,我建议先阅读一下python中的多重继承是奈德?巴奇尔德的强项。和这个深入研究Python和多重继承。
import unittest
from unittest import TestCase
"""
Showcase how to use mixins and multiple inheritance to write tests
"""
class BaseTest(TestCase):
"""
A base class to be inheritated by actuall test classes.
"""
def setUp(self): # 3
print("BaseTest:setUp called")
self.boo = "gladen sum"
@classmethod
def setUpClass(cls): # 1
print("BaseTest::setUpClass called")
cls.browser = 'musaka'
class FullDBMixin(object):
def setUp(self): # 5
super(FullDBMixin, self).setUp()
print("FullDBMixin::setUp called with instance attribute [boo] = %s" % self.boo)
class LoginMixin(object):
@classmethod
def setUpClass(cls): # 2
super(LoginMixin, cls).setUpClass()
print("LoginMixin::setUpClass called")
def setUp(self): # 4
super(LoginMixin, self).setUp()
print("LoginMixin::setUp called")
self.login()
def login(self):
print("LoginMixin::login called with class attribute [browser] %s" % self.browser)
# order of inheritance **matters**
class TestAuthontecation(LoginMixin, FullDBMixin , BaseTest):
def test_user_dashboard(self):
# test stuff without needing to setup the db or login the user
pass
if __name__ == '__main__':
unittest.main()
# georgi@georgi-laptop:~$ python test.py
# BaseTest::setUpClass called
# LoginMixin::setUpClass called
# BaseTest:setUp called
# FullDBMixin::setUp called with instance attribute [boo] = gladen sum
# LoginMixin::setUp called
# LoginMixin::login called with class attribute [browser] musaka
# .
# ----------------------------------------------------------------------
# Ran 1 test in 0.000s
# OKhttps://stackoverflow.com/questions/31421138
复制相似问题