2013-04-30 60 views
0

我可以将Test Suit作为python类/模块实现,以便拥有测试用例模块和测试套件模块。我想把测试套件的论点也传递给测试用例。Python中的单元测试套件

事情是这样的:

  • 测试套件模块:

    import unittest 
    
    class GPUScoringMatrixTestSuite(unittest.TestSuite): 
    
        def suite(): 
        suite = unittest.TestSuite()            
        suite.addTest(GPUScoringMatrixTestCase('PAM_350.txt')) 
        suite.addTest(GPUScoringMatrixTestCase('PAM_250.txt'))     
        self.run(suite) 
    
  • 测试用例模块:

    class GPUScoringMatrixTestCase(unittest.TestCase): 
    
        def __init__(self, matrix_file): 
        self.filename = matrix_file 
    
        @classmethod 
        def setUpClass(self): 
        self.matrix = GPUScoringMatrix(self.filename) 
    
        def test_sum_penalties(self): 
        sum = 0 
        for i in self.matrix.penalties: 
         sum += i 
        self.assertEqual(sum, -970, 'Iconsistence penalties between scoring matrices') 
    

的论点matrix_file没有工作过。 ..

回答

1

我不确定你要在这里做什么,似乎你正在编写代码来生成测试用例。为此,它可能有助于考虑Python的对象模型的令人难以置信的灵活性。特别是,你可以生成类型:

def make_testcase(matrix_file): 
    class MatrixTestCase(unittest.TestCase): 
     pass 
    MatrixTestCase.matrix_file = matrix_file 
    return MatrixTestCase 

PAM250Tests = make_testcase('PAM_250.txt') 
PAM350Tests = make_testcase('PAM_350.txt') 

我希望你没有与测试套件和单元测试的自动测试发现染指的话,但是,这两个的TestCase派生类自动拾取。

另一种方法是将矩阵文件作为常量存储在派生类中,将测试函数放入基类中。派生类然后派生自unittest.TestCase和附加的基类。

0

我也不明白第一部分,但我已经尝试从测试套件传递参数给测试用例。你正在改变__init__,但是,旧的__init__正在做一些重要的事情,需要重新实现。

  • 测试套件模块:

    import unittest 
    
    class GPUScoringMatrixTestSuite(unittest.TestSuite): 
    
    def suite(): 
        suite = unittest.TestSuite()            
        suite.addTest(GPUScoringMatrixTestCase('test_sum_penalties', 'PAM_350.txt')) 
        suite.addTest(GPUScoringMatrixTestCase('test_sum_penalties', 'PAM_250.txt'))     
        self.run(suite) 
    
  • 测试用例模块:

    class GPUScoringMatrixTestCase(unittest.TestCase): 
    
        def __init__(self, test_name, matrix_file): 
         #Preform duties of old __init__ 
         super(GPUScoringMatrixTestCase, self).__init__(test_name) 
         #Implement custom __init__ functionality 
         self.filename = matrix_file 
    
        @classmethod 
        def setUpClass(self): 
         self.matrix = GPUScoringMatrix(self.filename) 
    
        def test_sum_penalties(self): 
         sum = 0 
         for i in self.matrix.penalties: 
          sum += i 
         self.assertEqual(sum, -970, 'Iconsistence penalties between scoring matrices')