如果测试通过,我如何包含其他测试?

Phi*_*ham 3 python unit-testing nose system-testing

我正在使用nose一些系统测试,其中一个是测试(配置)文件是否存在.如果这个文件存在,我想对它运行一些额外的测试.如果没有,我想跳过一堆测试.

nose如果主要测试通过,最好的方法包括额外的测试?

小智 5

您可以在特定的TestCase中使用setUp方法中的skipTest,例如:

import os
from unittest import TestCase

class MyTest(TestCase):
    def setUp(self):
        if not os.path.exists('configfile'):
            return self.skipTest('config file not found')

    def test01(self):
        # Do something with the file
        with open('configfile') as fd:
            self.assertEqual(fd.readlines().__len__(), 0)
Run Code Online (Sandbox Code Playgroud)

如果配置文件不存在,测试test01将不会运行.