具有可变数量测试的 PyUnit

发布于 2024-09-07 13:59:01 字数 1118 浏览 3 评论 0原文

我想做的是创建一个文件夹,人们可以在其中放入文件进行测试,并让 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 技术交流群。

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

发布评论

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

评论(3

风渺 2024-09-14 13:59:01

您可以使用 nose 支持 测试生成器。这看起来像这样:

def test_comparison():
    for file in os.listdir(DIRECTORY):
        yield run_cmp_test, file

def run_cmp_test(file):
    # Run the tests

或者你也可以在 py.test 中 funcargs

You could use nose support for test generators. This would look something like this:

def test_comparison():
    for file in os.listdir(DIRECTORY):
        yield run_cmp_test, file

def run_cmp_test(file):
    # Run the tests

Alternatively you could also funcargs in py.test.

缱绻入梦 2024-09-14 13:59:01

好吧,我想我找到了问题的答案。我想说我正在使用全局命名空间,但由于某种原因,我通常对命名空间的了解在表面上是平淡的。

class TestImage(unittest.TestCase)
    pass #Do stuff, as well as using the filename variable

def suite():
    return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestImage),
])

if __name__ == "__main__":
    for filename in os.listdir(sys.path[0]):
        unittest.TextTestRunner(verbosity=2).run(suite())

这将为目录中的每个文件名运行一个测试用例。 (和文件夹名称)。虽然我不知道为什么我可以在单元测试类中使用文件名。它一定与这些宏加载和运行类的方式有关。

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.

class TestImage(unittest.TestCase)
    pass #Do stuff, as well as using the filename variable

def suite():
    return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestImage),
])

if __name__ == "__main__":
    for filename in os.listdir(sys.path[0]):
        unittest.TextTestRunner(verbosity=2).run(suite())

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.

魔法唧唧 2024-09-14 13:59:01

您可以使用 testscenarios 模块来完成此操作,如下所示:

class MyTest(unittest.TestCase):

    scenarios = [os.listdir(sys.path[0])]

    ...

You can do this with the testscenarios, module, something like this:

class MyTest(unittest.TestCase):

    scenarios = [os.listdir(sys.path[0])]

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