2014-09-21 64 views
0

我有一套鼻子測試,我用它來測試一塊硬件。例如,下面的測試關注的是系統測試報警每種模式:爲不同的測試目標配置鼻子測試

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() 

這是您認爲有效的方法嗎?

回答

0

解決類似問題的另一種方式是從以下列方式插件屬性濫用@attr裝飾:

from nose.plugins.attrib import attr 

max_alarm_time = 10 

# generate tests for testing each mode 
@attr(hardware='old') 
@attr(modes = ("start","stop","restart","stage1","stage2")) 
def test_generator_old(): 
    for m in test_generator_old.__dict__['modes']: 
     yield check_alarm, m, max_alarm_time 

@attr(hardware='new') 
@attr(modes = ("start","stop","restart","stage1","stage2", "stage3","stage4")) 
def test_generator_new(): 
    for m in test_generator_new.__dict__['modes']: 
     yield check_alarm, m, max_alarm_time 


# test alarm for a mode 
def check_alarm(m, max_alarm_time): 
    print "mode=", m 

您可以立即「舊」與「新」之間進行切換,就像這樣:

$ nosetests modes_test.py -a hardware=new -v 
modes_test.test_generator_new('start', 10) ... ok 
modes_test.test_generator_new('stop', 10) ... ok 
modes_test.test_generator_new('restart', 10) ... ok 
modes_test.test_generator_new('stage1', 10) ... ok 
modes_test.test_generator_new('stage2', 10) ... ok 
modes_test.test_generator_new('stage3', 10) ... ok 
modes_test.test_generator_new('stage4', 10) ... ok 

---------------------------------------------------------------------- 
Ran 7 tests in 0.020s 

OK 

而且老一: $ nosetests modes_test.py -a硬件=老-v

modes_test.test_generator_old('start', 10) ... ok 
modes_test.test_generator_old('stop', 10) ... ok 
modes_test.test_generator_old('restart', 10) ... ok 
modes_test.test_generator_old('stage1', 10) ... ok 
modes_test.test_generator_old('stage2', 10) ... ok 

---------------------------------------------------------------------- 
Ran 5 tests in 0.015s 

OK 

另外,雖然我還沒有玩過那麼多,nose-testconfig可以幫助你做同樣的伎倆。

+0

在我的所有測試中使用它們,在一個地方定義我的模式會更好嗎?例如,如果新硬件的模式發生變化,那麼我只需要在一個地方更改定義,而不必更改所有測試。 – Baz 2014-09-22 05:06:03

+0

它涉及到測試/硬件類型的比例。我看到你所做的一切都沒有錯 - 只要它適合你! – Oleksiy 2014-09-22 06:32:09