2015-10-16 112 views
1

我只是試圖執行下面一個簡單的測試用例列表,缺少必需的位置參數 - nosetests

# testinheritence.py 

import unittest 

class heleClass(object): 
    def execute_test_plan(self, test_plan): 
     self.assertEqual("a", "a") 


class TestParent(heleClass): 
    def testName(self): 
     test_plan = {"a": 1} 
     self.execute_test_plan(test_plan) 


class SqlInheritance(unittest.TestCase, TestParent): 
    print ("get into inheritance") 


if __name__ == "__main__": 
    unittest.main() 

然後用這個命令來測試它:「nosetests3 -s testinheritence.py」但我堅持遇到這些異常,它抱怨,

====================================================================== 
ERROR: testinheritence.TestParent.execute_test_plan 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/usr/lib/python3/dist-packages/nose/case.py", line 198, in runTest 
    self.test(*self.arg) 
TypeError: execute_test_plan() missing 1 required positional argument: 'test_plan' 

====================================================================== 
ERROR: testinheritence.TestParent.testName 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/usr/lib/python3/dist-packages/nose/case.py", line 198, in runTest 
    self.test(*self.arg) 
    File "/home/dave/scripts/testinheritence.py", line 16, in testName 
    self.execute_test_plan(test_plan) 
    File "/home/dave/scripts/testinheritence.py", line 10, in execute_test_plan 
    self.assertEqual("a", "a") 
AttributeError: 'TestParent' object has no attribute 'assertEqual' 

---------------------------------------------------------------------- 
Ran 4 tests in 0.003s 

與「蟒蛇-m單元測試testinheritence」來運行它,測試用例會順利通過,我GOOGLE了這一點,但還沒收到approache修復它,有什麼我錯過了嗎?任何迴應都非常受歡迎!

回答

1

這裏有幾個問題。你的heleClass不是一個合適的單元測試類(你使用object作爲你的父類,因此它沒有self.assertEqual()方法。此外,nose認爲「execute_test_plan」是一個測試,並將其稱爲測試的一部分,並且它失敗了,因爲它需要一個自變量嘗試標記execute_test_plan@nottest

import unittest 
from nose.tools import nottest 

class heleClass(unittest.TestCase): 
    @nottest 
    def execute_test_plan(self, test_plan): 
     self.assertEqual("a", "a") 


class TestParent(heleClass): 
    def testName(self): 
     test_plan = {"a": 1} 
     self.execute_test_plan(test_plan)   

if __name__ == "__main__": 
    unittest.main() 
+0

感謝解決我的問題完全,謝謝! – jungler

相關問題