具有可变数量测试的 PyUnit
我想做的是创建一个文件夹,人们可以在其中放入文件进行测试,并让 pyunit 自动扩展以便将测试作为单独的测试运行。目前,我正在做的是:
class TestName(unittest.testcase):
def setUp(self):
for file in os.listdir(DIRECTORY):
# Setup Tests
def test_comparison(self):
for file in os.listdir(DIRECTORY):
# Run the tests
def suite():
return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestName),
])
if __name__ == "__main__":
unittest.TextTestRunner(verbosity=2).run(suite())
显然存在很多问题,例如如果任何测试失败,整个事情都会失败等等。我想做的是设置 pyunit,以便它将运行测试(或测试用例),对于放置在目录中的每个文件。现在,我想我可以做的是创建上面列出的类,仅使用两种方法,但为了成功地做到这一点,我必须添加一个上下文参数,例如:
def setUp(self, filepath):
# Do stuff
然后我可以将循环放入代码的主要块,并像这样运行测试:
def suite():
return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestName, file),
])
if __name__ == "__main__":
for file in DIRECTORY:
unittest.TextTestRunner(verbosity=2).run(suite(file))
但我不知道如何在不几乎重写漏洞的unittest.TestCase()类的情况下做到这一点。有什么方法可以做到这一点,或者有没有其他方法可以根据文件夹中有多少文件来动态扩展测试/测试用例集?谢谢。
What I would like to do, is create a folder, where people can put in a file for testing, and have pyunit automatically expand in order to run the test as a separate test. Currently, what I'm doing is:
class TestName(unittest.testcase):
def setUp(self):
for file in os.listdir(DIRECTORY):
# Setup Tests
def test_comparison(self):
for file in os.listdir(DIRECTORY):
# Run the tests
def suite():
return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestName),
])
if __name__ == "__main__":
unittest.TextTestRunner(verbosity=2).run(suite())
Obviously there is many problems with that, such as if any test fails, the whole thing fails, etc. etc. What I would like to do, is set up pyunit so that it will run a test (or a test case), for each file that is placed in the directory. Now, what I'm thinking I could do is create the class listed above, with just the two methods, but in order to do that successfully, I would have to add in a context parameter, eg:
def setUp(self, filepath):
# Do stuff
Then I could put the loop in the main chunk of code, and run the tests like this:
def suite():
return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestName, file),
])
if __name__ == "__main__":
for file in DIRECTORY:
unittest.TextTestRunner(verbosity=2).run(suite(file))
But I'm not sure how to do that without nearly rewriting the hole unittest.TestCase() class. Is there any way I could do that, or is there any other way I can geta dynamically expanding set of tests/test cases depending on how many files are in a folder? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 nose 支持 测试生成器。这看起来像这样:
或者你也可以在 py.test 中 funcargs 。
You could use nose support for test generators. This would look something like this:
Alternatively you could also funcargs in py.test.
好吧,我想我找到了问题的答案。我想说我正在使用全局命名空间,但由于某种原因,我通常对命名空间的了解在表面上是平淡的。
这将为目录中的每个文件名运行一个测试用例。 (和文件夹名称)。虽然我不知道为什么我可以在单元测试类中使用文件名。它一定与这些宏加载和运行类的方式有关。
Okay, I think I found an answer to my question. I would say that I was using global namespaces, but for some reason, my usual knowledge of namespaces fell flat on it's face.
This will run a test case for every filename in the directory. (and folder name). Although I have no idea why I can use filename in the unittest class. It must have something to do with the way those macros load and run the class.
您可以使用 testscenarios 模块来完成此操作,如下所示:
You can do this with the testscenarios, module, something like this: