Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于一组自动测试,只运行安装程序一次_Python_Unit Testing - Fatal编程技术网

Python 对于一组自动测试,只运行安装程序一次

Python 对于一组自动测试,只运行安装程序一次,python,unit-testing,Python,Unit Testing,我的Python版本是2.6 我只想执行一次测试设置方法,因为我在那里做了所有测试都需要的事情 我的想法是创建一个布尔变量,在第一次执行后将其设置为“true”,然后禁用对setup方法的多个调用 class mySelTest(unittest.TestCase):     setup_done = False     def setUp(self):         print str(self.setup_done)                      if self.setup_

我的Python版本是2.6

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

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

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 ---

为什么这不起作用?我遗漏了什么吗?

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

不要尝试重复对安装程序的调用,只需调用一次即可

例如:

class MyClass(object):
    ...

def _set_up():
    code to do one-time setup

_set_up()

这将在模块首次加载时调用_set_up()。我已经将其定义为模块级函数,但您也可以将其作为MyClass的类方法。

将所有需要设置的代码放在MyEltest之外

setup_done = False

class mySelTest(unittest.TestCase):

    def setUp(self):
        print str(setup_done)

        if setup_done:
            return

        setup_done = True
        print str(setup_done)
另一种可能性是在
setUp()
中实例化一个单例类,它只运行
\uuuuu new\uuuuuu
代码一次,并为其余调用返回对象实例。 见:

不过你的方法也行。

是正确的,但这里有一个例子可以避免我发现的一些常见错误,例如当
TestCase
unittest.TestCase
的子类时,在
setUpClass()中不调用
super()
(如
django.test
falcon.testing

的文档没有提到在这种情况下需要调用
super()
。如果不这样做,您将得到一个错误,如中所示


setup_done是类变量,而不是实例变量

您正在将其作为实例变量引用:

self.setup\u done

但您需要将其作为类变量引用:

mySelTest.setup\u完成

以下是更正后的代码:

class mySelTest(unittest.TestCase):
    setup_done = False

    def setUp(self):
        print str(mySelTest.setup_done)

        if mySelTest.setup_done:
            return
        mySelTest.setup_done = True
        print str(mySelTest.setup_done)

如果你因为需要加载一些数据进行测试而来到这里。。。 如果您使用的是Django 1.9+,请选择:


我正在使用Python3,发现
cls
引用也可以在
setup
方法中使用,因此以下方法可以工作:

class TestThing(unittest.TestCase):

  @classmethod
  def setUpClass(cls):
    cls.thing = Thing() # the `thing` is only instantiated once

  def setup(self):
    self.thing = cls.thing # ...but set on each test case instance

  def test_the_thing(self):
    self.assertTrue(self.thing is not None)

对于python>3,可以通过定义
unittest.TestResult类的
startTestRun
stopTestRun
来实现。回答

Unittest为每个测试创建单独的实例不要这样做。实施其他一些机制。但不要试图更改
设置的含义。感谢您的回复,这可能是重复的。因为我使用的是Python 2.6.6 setUpClass,所以不可用。@JohnM.:您可以下载unittest2 backport包,并在旧版Python-dist上获取所有新内容。问题涉及Python 2,但是,由于答案对Python 3也是有效的,我更改了URL,因为Python 2已被弃用。请注意,只有当
TestCase
unittest.TestCase
的子类时,这才相关。您无法访问安装程序中的cls,这是一个实例方法(小写的setUp()不会被调用,因为它不是unittest安装方法,也不是以“test”开头的方法,这可能是您没有得到错误的原因)但是,一旦您在setUpClass中定义了类变量,您可以在所有实例方法中调用self.thing,@khuang834。你说得对。这是我的疏忽。
class mySelTest(unittest.TestCase):
    setup_done = False

    def setUp(self):
        print str(mySelTest.setup_done)

        if mySelTest.setup_done:
            return
        mySelTest.setup_done = True
        print str(mySelTest.setup_done)
class MyTests(TestCase):

    @classmethod
    def setUpTestData(cls):
        # Set up data for the whole TestCase
        cls.foo = Foo.objects.create(bar="Test")

    def test1(self):
        self.assertEqual(self.foo.bar, 'Test') 
class TestThing(unittest.TestCase):

  @classmethod
  def setUpClass(cls):
    cls.thing = Thing() # the `thing` is only instantiated once

  def setup(self):
    self.thing = cls.thing # ...but set on each test case instance

  def test_the_thing(self):
    self.assertTrue(self.thing is not None)