2017-01-16 78 views
3

爲了減少鍋爐板代碼,我想到了在類Tester中爲所有無參數方法生成測試用例的想法。如何自動生成無參數方法的測試用例?

在運行py.test,它只能識別使用setattr(Tester,'test_' + name, member)

也許py.test已經檢查了類測試儀的靜態書面測試用例(test_a,test_b),但不是動態創建測試用例在調用setUpClass之前調用'test_ *'的方法?任何提示如何讓這個運行?

import inspect 
import unittest 


class Testee: 
    def a(self): 
     print('a') 

    def b(self): 
     print('b')  

    #... 

    #... 
    def z(self): 
     print('z') 

class Tester(unittest.TestCase): 

    @classmethod 
    def setUpClass(cls): 
     testee = Testee() 
     for name, member in inspect.getmembers(object=testee, predicate=inspect.ismethod or inspect.iscoroutine): 
      if len(inspect.signature(member).parameters): 
       print(str(inspect.signature(member).parameters)) 
       setattr(Tester,'test_' + name, member) 
      if inspect.isfunction(member) or inspect.ismethod(member): 
       setattr(Tester,'test_' + name, member) 
      elif inspect.iscoroutinefunction(member): 
       setattr(Tester,'test_' + name, functools.partialmethod(TestInstrument.run_coro, member)) 
      else: 
       print(member) 
     return super().setUpClass() 

    def test_a(self): 
     Tester.testee.a() 

    def test_b(self): 
     Tester.testee.b() 

=============================測試環節開始========= ==================== platform win32 - Python 3.5.1,pytest-2.9.2,py-1.4.31,pluggy-0.3.1 - c: \程序 文件\ python35 \ python.exe cachedir:.cache ROOTDIR:C:\測試,ini文件: 收集2項

sandbox.py::Tester::test_a PASSED sandbox.py::Tester::test_b PASSED

========================== 2秒0.03秒============== =============

EDIT:如果我移動代碼setupClass到全球範圍內(在類外),然後py.test檢測並運行自動生成測試用例。

+0

你可以請張貼你實例化測試類的地方嗎?此外,請在您的setattr中使用'cls'而不是'Tester'。 –

+0

@Vincenzzzochi py.test爲我做的瞬間。 – goldcode

回答

0

爲了詳細闡述我的'編輯',一個選項就是如此。我對解決方案感到不滿,因爲全局代碼執行會對副作用和其他微妙的錯誤開放。任何關於如何將這個問題轉化爲Tester範圍的建議?

import inspect 
import unittest 
import functools 
def auto_generate_test_cases(Tester, Testee): 
    def run(self, fn): 
     fn(Tester._testee) 
    for name, member in inspect.getmembers(
     object=Testee, predicate=inspect.isfunction or inspect.iscoroutine): 
     if len(inspect.signature(member).parameters) == 1:    
      setattr(Tester,'test_' + name, functools.partialmethod(run, member)) 

class Testee: 
    def __init__(self): 
     self._a = 'a' 
    def a(self): 
     print(self._a) 
    def ab(self, a, b): 
     print('a') 
    def b(self): 
     print('b')  
    def h(self): 
     print('h')  
    async def q(self): 
     print('async q') 
    #... 

    #... 
    def z(self): 
     print('z') 

class Tester(unittest.TestCase): 
    _testee = Testee() 
auto_generate_test_cases(Tester, Testee) 

py.test。輸出:

C:\tests>py.test sandbox.py --verbose 
============================= test session starts ============================= 
platform win32 -- Python 3.5.1, pytest-2.9.2, py-1.4.31, pluggy-0.3.1 -- c:\program files\python35\python.exe 
cachedir: .cache 
rootdir: C:\\tests, inifile: 
collected 5 items 

sandbox.py::Tester::test_a PASSED 
sandbox.py::Tester::test_b PASSED 
sandbox.py::Tester::test_h PASSED 
sandbox.py::Tester::test_q PASSED 
sandbox.py::Tester::test_z PASSED 

========================== 5 passed in 0.07 seconds ===========================