【发布时间】:2021-09-17 13:27:35
【问题描述】:
问题陈述
我正在尝试为一个函数构建单元测试,该函数对其接收的输入执行一系列算术运算。这些操作是在函数之外配置的(在这种情况下是在一个包常量中)。
我的问题是 我可以编写应该通过的测试,但是如果外部配置发生更改,测试将会中断,或者测试将主要是要测试的函数中代码的重复。强>
我能想到的编写测试的两种方式是:
-
“假定”特定配置的测试。问题是测试很脆弱,如果我更改配置就会停止工作。 (参见下面的 test_example1)
- 假设某些操作并应用于输入,创建预期结果
- 调用函数进行测试
- 将预期结果与要测试的函数返回的实际结果进行比较
-
我可以构建一个使用配置来计算预期结果的测试。问题是,首先,测试依赖于函数外部的配置常量,其次,测试的代码与测试的代码非常相似,感觉不对。 (参见下面的 test_example2)
- 根据配置常量创建处理输入的预期结果
- 调用函数进行测试
- 将预期结果与要测试的函数返回的实际结果进行比较
您能帮我找出对执行外部配置的操作的函数进行单元测试的正确方法吗?
示例代码
import unittest
import operator
OPS = {'+':operator.add,
'-':operator.sub}
DIRECTIVES = {'calculated_field':[('+','field1'),
('+','field2')]}
def example(input_dict):
output_dict = {}
for item,calcs in DIRECTIVES.items():
output_dict[item] = 0
for operation, field in calcs:
output_dict[item] = OPS[operation](output_dict[item],input_dict[field])
return output_dict
class TestExample(unittest.TestCase):
item1 = 'field1'
value1 = 10
item2 = 'field2'
value2 = 20
item3 = 'field3'
value3 = 5
def setUp(self):
self.input_dict = {self.item1:self.value1,
self.item2:self.value2,
self.item3:self.value3}
def test_example_option1(self):
expected_result = {'calculated_field':self.value1+self.value2}
actual_result = example(self.input_dict)
self.assertDictEqual(expected_result,actual_result)
def test_example_option2(self):
expected_result = {}
for item,calcs in DIRECTIVES.items():
expected_result[item] = 0
for operation, field in calcs:
expected_result[item] = OPS[operation](expected_result[item],self.input_dict[field])
actual_result = example(self.input_dict)
self.assertDictEqual(expected_result,actual_result)
【问题讨论】:
标签: python unit-testing python-unittest