相关疑难解决方法(0)

单元测试setUp/tearDown进行多次测试

是否有一个在测试场景的开头/结尾触发的函数?函数setUp和tearDown在每次测试之前/之后触发.

我通常想拥有这个:

class TestSequenceFunctions(unittest.TestCase):

    def setUpScenario(self):
        start() #launched at the beginning, once

    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)

    def tearDownScenario(self):
        end() #launched at the end, once
Run Code Online (Sandbox Code Playgroud)

现在,这些setUp和tearDown是单元测试并在我的所有场景中传播(包含许多测试),一个是第一个测试,另一个是最后一个测试.

python unit-testing

107
推荐指数
3
解决办法
7万
查看次数

使用Python unittest缓存setUp()的结果

我目前有一个看起来像的unittest.TestCase ..

class test_appletrailer(unittest.TestCase):
    def setup(self):
        self.all_trailers = Trailers(res = "720", verbose = True)

    def test_has_trailers(self):
        self.failUnless(len(self.all_trailers) > 1)

    # ..more tests..
Run Code Online (Sandbox Code Playgroud)

这工作正常,但Trailers()调用大约需要2秒才能运行..鉴于setUp()在每次测试运行之前调用,测试现在需要大约10秒才能运行(只有3个测试函数)

self.all_trailers在测试之间缓存变量的正确方法是什么?

删除setUp函数,并执行..

class test_appletrailer(unittest.TestCase):
    all_trailers = Trailers(res = "720", verbose = True)
Run Code Online (Sandbox Code Playgroud)

..works,但它声称"在0.000s中进行3次测试"这是不正确的..我能想到的另一种方法是拥有一个cache_trailers全局变量(它工作正常,但相当可怕):

cache_trailers = None
class test_appletrailer(unittest.TestCase):
    def setUp(self):
        global cache_trailers
        if cache_trailers is None:
            cache_trailers = self.all_trailers = all_trailers = Trailers(res = "720", verbose = True)
        else:
            self.all_trailers = cache_trailers
Run Code Online (Sandbox Code Playgroud)

python unit-testing

9
推荐指数
3
解决办法
4900
查看次数

所有测试后,Python单元测试运行功能

我需要通过ssh在python上测试smth.我不想为每个测试做ssh连接,因为它很长,我写了这个:

class TestCase(unittest.TestCase):
    client = None
    def setUp(self):
        if not hasattr(self.__class__, 'client') or self.__class__.client is None:
            self.__class__.client = paramiko.SSHClient()
            self.__class__.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.__class__.client.connect(hostname=consts.get_host(), port=consts.get_port(), username=consts.get_user(),
                                password=consts.get_password())

    def test_a(self):
        pass

    def test_b(self):
        pass

    def test_c(self):
        pass

    def disconnect(self):
        self.__class__.client.close()
Run Code Online (Sandbox Code Playgroud)

和我的跑步者

if __name__ == '__main__':
    suite = unittest.TestSuite((
        unittest.makeSuite(TestCase),
    ))
    result = unittest.TextTestRunner().run(suite)
    TestCase.disconnect()
    sys.exit(not result.wasSuccessful())
Run Code Online (Sandbox Code Playgroud)

在这个版本中我得到错误TypeError: unbound method disconnect() must be called with TestCase instance as first argument (got nothing instead).那么在所有测试通过后我怎么能断断续续?最诚挚的问候.

python ssh unit-testing python-unittest

9
推荐指数
2
解决办法
5547
查看次数

标签 统计

python ×3

unit-testing ×3

python-unittest ×1

ssh ×1