2017-01-16 57 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 ===========================