我有一组鼻子测试,用于测试硬件。例如,下面的测试涉及测试系统上每种模式的警报:
import target
modes = ("start","stop","restart","stage1","stage2")
max_alarm_time = 10
# generate tests for testing each mode
def test_generator():
for m in modes:
yield check_alarm, m, max_alarm_time
# test alarm for a mode
def check_alarm(m, max_alarm_time):
target.set_mode(m)
assert target.alarm() < max_alarm_time
我的大多数测试都有这种外观,我正在为modes
系统上的所有人测试特定功能。
我现在希望使用相同的测试集来测试具有两种额外模式的新硬件:
modes = ("start","stop","restart","stage1","stage2","stage3","stage4")
当然,我希望我的测试仍然适用于旧硬件。在运行自动化测试时,我需要对测试环境中连接的硬件进行硬编码。
我相信最好的方法是创建一个 paramaters.py 模块,如下所示:
def init(hardware):
global max_alarm_time
global modes
max_alarm_time = 10
if hardware == "old":
modes = ("start","stop","restart","stage1","stage2")
elif hardware == "new":
modes = ("start","stop","restart","stage1","stage2","stage3","stage4")
test_alarms.py 现在看起来像这样:
import target
import parameters
# generate tests for testing each mode
def test_generator():
for m in parameters.modes:
yield check_alarm, m, parameters.max_alarm_time
# test alarm for a mode
def check_alarm(m, max_alarm_time):
target.set_mode(m)
assert target.alarm() < max_alarm_time
然后在我的主要内容中,我有以下内容:
import nose
import parameters
parameters.init("new")
nose.main()
您认为这是一种有效的方法吗?