为每个方法或在 TestCase 的开头和结尾执行 setUp 和 tearDown 方法 运行
Do setUp and tearDown methods run for each method or at the beginning and at the end of TestCase
作为同一 TestCase 成员的测试方法是否会相互影响?
在 python unittest 中,我试图了解如果我在测试方法中更改变量,其他测试方法中的变量是否会更改。或者为每个方法执行 setUp 和 tearDown 方法 运行,然后为每个方法重新设置变量?
我是说
AsdfTestCase(unittest.TestCase):
def setUp(self):
self.dict = {
'str': 'asdf',
'int': 10
}
def tearDown(self):
del self.dict
def test_asdf_1(self):
self.dict['str'] = 'test string'
def test_asdf_2(self):
print(self.dict)
所以我想问 test_asdf_2() 将打印哪个输出
'asdf' 或 'test_string'
每个测试用例都是孤立的。设置方法是每个测试用例之前运行,拆卸是每个测试用例之后运行。
所以回答你的问题,如果你改变你的测试用例中的一个变量,它不会影响其他测试用例。
您编写的测试代码走在了正确的道路上。当你自己做的时候,它总是一个更好的学习体验。不过,这就是你的答案。
示例代码:
import unittest
class AsdfTest(unittest.TestCase):
def setUp(self):
print "Set Up"
self.dict = {
'str': 'asdf',
'int': 10
}
def tearDown(self):
print "Tear Down"
self.dict = {}
def test_asdf_1(self):
print "Test 1"
self.dict['str'] = 'test string'
print self.dict
def test_asdf_2(self):
print "Test 2"
print self.dict
if __name__ == '__main__':
unittest.main()
输出:
Set Up
Test 1
{'str': 'test string'}
Tear Down
.Set Up
Test 2
{}
Tear Down
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
可以看到每次测试前的设置方法是运行。那么每次测试后的拆解方法是运行
是的,在测试用例 class 中的每个测试(即名称中以 'test' 开头的函数)之前设置和拆卸 运行。考虑这个例子:
# in file testmodule
import unittest
class AsdfTestCase(unittest.TestCase):
def setUp(self) : print('setUp called')
def tearDown(self) : print('tearDown called')
def test_asdf_1(self): print( 'test1 called' )
def test_asdf_2(self): print( 'test2 called' )
从命令行调用它:
$ python3 -m unittest -v testmodule
test_asdf_1 (testmodule.AsdfTestCase) ... setUp called
test1 called
tearDown called
ok
test_asdf_2 (testmodule.AsdfTestCase) ... setUp called
test2 called
tearDown called
ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
(因此,是的,在您的示例中,它会弹出 'asdf',因为重新执行设置,覆盖由测试 2 引起的更改)
作为同一 TestCase 成员的测试方法是否会相互影响?
在 python unittest 中,我试图了解如果我在测试方法中更改变量,其他测试方法中的变量是否会更改。或者为每个方法执行 setUp 和 tearDown 方法 运行,然后为每个方法重新设置变量?
我是说
AsdfTestCase(unittest.TestCase):
def setUp(self):
self.dict = {
'str': 'asdf',
'int': 10
}
def tearDown(self):
del self.dict
def test_asdf_1(self):
self.dict['str'] = 'test string'
def test_asdf_2(self):
print(self.dict)
所以我想问 test_asdf_2() 将打印哪个输出 'asdf' 或 'test_string'
每个测试用例都是孤立的。设置方法是每个测试用例之前运行,拆卸是每个测试用例之后运行。
所以回答你的问题,如果你改变你的测试用例中的一个变量,它不会影响其他测试用例。
您编写的测试代码走在了正确的道路上。当你自己做的时候,它总是一个更好的学习体验。不过,这就是你的答案。
示例代码:
import unittest
class AsdfTest(unittest.TestCase):
def setUp(self):
print "Set Up"
self.dict = {
'str': 'asdf',
'int': 10
}
def tearDown(self):
print "Tear Down"
self.dict = {}
def test_asdf_1(self):
print "Test 1"
self.dict['str'] = 'test string'
print self.dict
def test_asdf_2(self):
print "Test 2"
print self.dict
if __name__ == '__main__':
unittest.main()
输出:
Set Up
Test 1
{'str': 'test string'}
Tear Down
.Set Up
Test 2
{}
Tear Down
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
可以看到每次测试前的设置方法是运行。那么每次测试后的拆解方法是运行
是的,在测试用例 class 中的每个测试(即名称中以 'test' 开头的函数)之前设置和拆卸 运行。考虑这个例子:
# in file testmodule
import unittest
class AsdfTestCase(unittest.TestCase):
def setUp(self) : print('setUp called')
def tearDown(self) : print('tearDown called')
def test_asdf_1(self): print( 'test1 called' )
def test_asdf_2(self): print( 'test2 called' )
从命令行调用它:
$ python3 -m unittest -v testmodule
test_asdf_1 (testmodule.AsdfTestCase) ... setUp called
test1 called
tearDown called
ok
test_asdf_2 (testmodule.AsdfTestCase) ... setUp called
test2 called
tearDown called
ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
(因此,是的,在您的示例中,它会弹出 'asdf',因为重新执行设置,覆盖由测试 2 引起的更改)