2016-03-29 22 views
0

私は、自動的にテストケースを作成し、nosetestを使ってテストケースを実行しようとしています。コマンドを実行すると自動的にテストケースを作成する

テストが正常に動作: のpython -m unittestのtest_auto1 のpython -m unittestのtest_auto1.TestAuto.test_two

私はnosetestを使用してテストを実行しようとする場合は、それが特定の条件に失敗しました:

1)nosetests test_auto1 - エラーで
2)nosetestsのtest_auto1失敗:TestAutoを - 正常に動作
3)nosetests test_auto1:TestAuto.test_one - エラーで失敗

ここ

は、テストコードです:単一のテストを実行している間

import unittest 

def generator(test_class, a, b): 
    def test(self): 
     self.assertEqual(a, b) 
    return test 

def add_test_methods(test_class): 
    #First element of list is variable "a", then variable "b", then name of test case that will be used as suffix. 
    test_list = [[2,3, 'one'], [5,5, 'two'], [0,0, 'three']] 
    for case in test_list: 
     test = generator(test_class, case[0], case[1]) 
     setattr(test_class, "test_%s" % case[2], test) 


class TestAuto(unittest.TestCase): 
    def setUp(self): 
     print 'Setup' 
     pass 

    def tearDown(self): 
     print 'TearDown' 
     pass 

add_test_methods(TestAuto) 

if __name__ == '__main__': 
    unittest.main(verbosity=1) 

エラーです:

====================================================================== 
ERROR: Failure: ValueError (no such test method in <class 'test_auto2.TestAuto'>: test) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\Python27\lib\site-packages\nose-1.3.1-py2.7.egg\nose\loader.py", line 516, in makeTest 
    return self._makeTest(obj, parent) 
    File "C:\Python27\lib\site-packages\nose-1.3.1-py2.7.egg\nose\loader.py", line 570, in _makeTest 
    return parent(obj.__name__) 
    File "C:\Python27\lib\unittest\case.py", line 189, in __init__ 
    (self.__class__, methodName)) 
ValueError: no such test method in <class 'test_auto2.TestAuto'>: test 

---------------------------------------------------------------------- 
Ran 1 test in 0.001s 

FAILED (errors=1) 

答えて

1

私が見る唯一の問題は、最も可能性の高いadd_test_methodsはテストとして解釈されていることです。私はnottestとしてそれをマークすると、上記と同じコードが期待通りに動作します:

from nose.tools import nottest 

@nottest 
def add_test_methods(test_class): 
.... 

そして今、それを実行している:

nosetests -v 
test_one (auto.TestAuto) ... FAIL 
test_three (auto.TestAuto) ... ok 
test_two (auto.TestAuto) ... ok 

====================================================================== 
FAIL: test_one (auto.TestAuto) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/opikalo/src/nose/auto.py", line 7, in test 
    self.assertEqual(a, b) 
AssertionError: 2 != 3 
-------------------- >> begin captured stdout << --------------------- 
Setup 

--------------------- >> end captured stdout << ---------------------- 

---------------------------------------------------------------------- 
Ran 3 tests in 0.001s 

FAILED (failures=1) 
+0

これは私が探していたが、それは確かに私があることが判明何かすること教えものではありませんとても役に立ちました...ありがとう –

関連する問題