2

For few days I'm trying to lern how to properly use unittest library in Python. I've got some basics but I've made myself a challenge - test two functions that I made before. These are the functions:

    def PatternMatch(self, message, number):
        ret_status = 2 # 'No match'
        ret_pattern = -1  

        for pattern in self.PatternList:
            if pattern.active == 1 and pattern.LA == number:
                try:
                    RegExp = re.compile(pattern.regex)
                if RegExp.match(message):
                    ret_status = 1
                    ret_pattern = pattern.ID
                    break

        return ret_status, ret_pattern


    def GetPattern(self, patternID):
        pattern = None
        db = Database()
        query = 'CALL PATTERN_GET'
        query += '(@sql_err_code, @my_status, @my_msg, %s)'
        ret = db.query(query, [patternID])
        if len(ret['data']) == 2:
            pattern = Pattern(ret['data'][1])
        else:
            pattern = Pattern()

        result = {'status': ret['data'][0]}
        return pattern, result

How should i start? I have mocked the database connection using the mox library and it works but what about the functionality? I appreciate any help.

1 Answer 1

5

Given the generality of the question only general advice is possible - basically you need to prepare test data &/or inputs that will perform the following:

  1. Code Coverage - force all your execution path down every decision path.
  2. Decision coverage - as above but via each way of making a decision - e.g. if a >= 0 needs testing with a < 0, a == 0 and a > 0.
  3. Corner case coverage - does the code work with very large, very small, very negative, no values.
  4. Exception coverage: Raise all the exceptions that the documentation says can be raised by the code inside try clauses and see if they are handled properly or should be allowed through.
  5. Always start each set of test data with a clear statement of what you are trying to achieve in that set of test data.
Sign up to request clarification or add additional context in comments.

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.