为一组自动化测试只运行一次 setUp [英] Run setUp only once for a set of automated tests

查看:43
本文介绍了为一组自动化测试只运行一次 setUp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Python 版本是 2.6.

My Python version is 2.6.

我只想执行一次测试 setUp 方法,因为我在那里做所有测试都需要的事情.

I would like to execute the test setUp method only once since I do things there which are needed for all tests.

我的想法是创建一个布尔变量,该变量将在第一次执行后设置为true",然后禁用对 setup 方法的多次调用.

My idea was to create a boolean variable which will be set to 'true' after the first execution and then disable more than one call to the setup method.

class mySelTest(unittest.TestCase):
    setup_done = False

    def setUp(self):
        print str(self.setup_done)
            
        if self.setup_done:
            return
        self.setup_done = True
        print str(self.setup_done)

输出:

False

True

--- Test 1 ---

False

True

--- Test 2 ---

为什么这不起作用?我错过了什么吗?

why is this not working? Did I miss anything?

推荐答案

您可以使用 setUpClass 定义每个测试套件只运行一次的方法.

You can use setUpClass to define methods that only run once per testsuite.

这篇关于为一组自动化测试只运行一次 setUp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆