Python unittest 是否在每次调用中重置 setUp 函数?
Does Python unittest reset the setUp function in each call?
我目前正在 class
上进行单元测试
class Employee:
"""Class to define an employee"""
def __init__(self, fname, lname, salary):
self.fname = fname
self.lname = lname
self.salary = salary
def give_raise(self, number=5000):
self.salary += number
我正在编写测试来测试我在 class 中定义的方法。第一个将通过默认函数参数增加员工薪水,第二个是自定义
from employee import Employee
import unittest
class TestEmployee(unittest.TestCase):
"""Test the employee class"""
def setUp(self):
"""
Create employees and test the results
"""
self.dave = Employee("dave", "stanton", 20000)
def test_default_raise(self):
self.dave.give_raise()
self.assertEqual(25000, self.dave.salary)
print(self.dave.salary)
def test_custom_raise(self):
self.dave.give_raise(10000)
self.assertEqual(30000, self.dave.salary)
print(self.dave.salary)
if __name__ == '__main__':
unittest.main()
我正在使用 SetUp 函数来实例化员工的实例 class。
我的问题是 self.dave.salary 的值是否会在每次方法调用时重置为基值?
30000
.25000
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
我预计第二次测试的值为 35000
来自docs:
“setUp() 和 tearDown() 方法允许您定义将在每个测试方法之前和之后执行的指令。”
我目前正在 class
上进行单元测试class Employee:
"""Class to define an employee"""
def __init__(self, fname, lname, salary):
self.fname = fname
self.lname = lname
self.salary = salary
def give_raise(self, number=5000):
self.salary += number
我正在编写测试来测试我在 class 中定义的方法。第一个将通过默认函数参数增加员工薪水,第二个是自定义
from employee import Employee
import unittest
class TestEmployee(unittest.TestCase):
"""Test the employee class"""
def setUp(self):
"""
Create employees and test the results
"""
self.dave = Employee("dave", "stanton", 20000)
def test_default_raise(self):
self.dave.give_raise()
self.assertEqual(25000, self.dave.salary)
print(self.dave.salary)
def test_custom_raise(self):
self.dave.give_raise(10000)
self.assertEqual(30000, self.dave.salary)
print(self.dave.salary)
if __name__ == '__main__':
unittest.main()
我正在使用 SetUp 函数来实例化员工的实例 class。
我的问题是 self.dave.salary 的值是否会在每次方法调用时重置为基值?
30000
.25000
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
我预计第二次测试的值为 35000
来自docs:
“setUp() 和 tearDown() 方法允许您定义将在每个测试方法之前和之后执行的指令。”