Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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_Python Unittest - Fatal编程技术网

Python单元测试、继承和成员变量传播

Python单元测试、继承和成员变量传播,python,python-unittest,Python,Python Unittest,我正在使用Python 2.6。假设我有两门课: class BaseTest(unittest.TestCase): def test_a(self): print 'test_a' self.random_variable = random_string() print self.random_variable class SubTest1(BaseTest): def test_b(self): print

我正在使用Python 2.6。假设我有两门课:

class BaseTest(unittest.TestCase):
    def test_a(self):
        print 'test_a'
        self.random_variable = random_string()
        print self.random_variable

class SubTest1(BaseTest):
    def test_b(self):
        print 'test_b'
        print self.random_variable
其中random_string()返回随机生成的20个字符的字符串

我希望能够访问
子测试1.test\u b
中的
self.random\u variable
。现在,test_b中的self.random_变量未定义

当运行
BaseTest
时,它会有自己唯一的随机字符串,但当运行
SubTest1
时,它会在
BaseTest
中生成相同的字符串。所有这些的输出理想情况下如下所示:

test_a
dgkwgmhkiszvmlhceved

test_a
akvjkskdmhfygsysgjci
test_b
akvjkskdmhfygsysgjci

这可能吗?

是的,您可以在每个测试用例之前运行的
设置
方法中定义变量(因此每个测试的变量不同):

如果您希望它保持不变,则需要使用
setUpClass

class BaseTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        self.random_variable = random_string()

    def test_a(self):
        print 'test_a'
        print self.random_variable

请注意,使用
@classmethod
标记
setUpClass
非常重要,因为它将方法从常规方法转换为类方法(接收类而不是实例作为第一个参数)。

是的,您可以在每个测试用例之前运行的
setUp
方法中定义变量(因此,每个测试的结果不同):

如果您希望它保持不变,则需要使用
setUpClass

class BaseTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        self.random_variable = random_string()

    def test_a(self):
        print 'test_a'
        print self.random_variable

请注意,使用
@classmethod
标记
setUpClass
非常重要,因为它将方法从常规方法转换为类方法(接收类而不是实例作为第一个参数)。

是的,您可以在每个测试用例之前运行的
setUp
方法中定义变量(因此,每个测试的结果不同):

如果您希望它保持不变,则需要使用
setUpClass

class BaseTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        self.random_variable = random_string()

    def test_a(self):
        print 'test_a'
        print self.random_variable

请注意,使用
@classmethod
标记
setUpClass
非常重要,因为它将方法从常规方法转换为类方法(接收类而不是实例作为第一个参数)。

是的,您可以在每个测试用例之前运行的
setUp
方法中定义变量(因此,每个测试的结果不同):

如果您希望它保持不变,则需要使用
setUpClass

class BaseTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        self.random_variable = random_string()

    def test_a(self):
        print 'test_a'
        print self.random_variable

请注意,使用
@classmethod
标记
setUpClass
非常重要,因为它可以将方法从常规方法转换为类方法(它接收类而不是实例作为第一个参数).

单元测试的原则之一是,每个测试都应该与其他测试隔离,并且独立于其他测试。如果您希望在多个测试中完成测试工作,请将其放入帮助函数中,并从测试中调用该函数:

class MyTests(unittest.TestCase):
    def do_common_stuff(self):
        self.random_variable = random_string()
        #... do more stuff you want in more than one test ...

    def test_a(self):
        self.do_common_stuff()

    def test_b(self):
        self.do_common_stuff()
        #... do more stuff ...

单元测试的原则之一是,每个测试都应该与其他测试隔离并独立于其他测试。如果您希望在多个测试中完成测试工作,请将其放入辅助函数中,并从测试中调用该函数:

class MyTests(unittest.TestCase):
    def do_common_stuff(self):
        self.random_variable = random_string()
        #... do more stuff you want in more than one test ...

    def test_a(self):
        self.do_common_stuff()

    def test_b(self):
        self.do_common_stuff()
        #... do more stuff ...

单元测试的原则之一是,每个测试都应该与其他测试隔离并独立于其他测试。如果您希望在多个测试中完成测试工作,请将其放入辅助函数中,并从测试中调用该函数:

class MyTests(unittest.TestCase):
    def do_common_stuff(self):
        self.random_variable = random_string()
        #... do more stuff you want in more than one test ...

    def test_a(self):
        self.do_common_stuff()

    def test_b(self):
        self.do_common_stuff()
        #... do more stuff ...

单元测试的原则之一是,每个测试都应该与其他测试隔离并独立于其他测试。如果您希望在多个测试中完成测试工作,请将其放入辅助函数中,并从测试中调用该函数:

class MyTests(unittest.TestCase):
    def do_common_stuff(self):
        self.random_variable = random_string()
        #... do more stuff you want in more than one test ...

    def test_a(self):
        self.do_common_stuff()

    def test_b(self):
        self.do_common_stuff()
        #... do more stuff ...

不幸的是,setUpClass是在2.7中添加的方法。我使用的是2.6。不幸的是,setUpClass是在2.7中添加的方法。我使用的是2.6。不幸的是,setUpClass是在2.7中添加的方法。我使用的是2.6。不幸的是,setUpClass是在2.7中添加的方法。我使用的是2.6。