2

Can I implement Test Suit as a python class/module so that I have Test Case module and Test Suite module. I want to pass a argument from testsuite to testcase too.

Something like this:

  • Test Suite module:

    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)
    
  • Test Case module:

    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')
    

The argument matrix_file isn't work too...

2 Answers 2

3

I'm not sure what you're trying to do here, it seems you are trying to write code to generate testcases. For that, maybe it helps taking into account the incredible flexibility of Python's object model. In particular, you can generate types:

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')

I would hope that you don't have to meddle with the test suite and unittest's automatic test discovery then, but that these two TestCase-derived classes are picked up automatically.

A different approach is that you store the matrix file as a constant in a derived class, putting the test functions in a base class. The derived class then derives from both unittest.TestCase and the additional base class.

Sign up to request clarification or add additional context in comments.

Comments

0

I also don't understand the first part, but I have tried passing arguments to testcases from test suites. You were going about it correctly with changing the __init__, however, the old __init__ was doing something important, that needs to be reimplemented.

  • TestSuite Module:

    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)
    
  • TestCase Module:

    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')
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.