Custom Test Result Reporting Using Log Levels

Custom Test Result Reporting Using Log Levels

Categorizing log output into different levels allows you to decide whether you want to have more or less detailed messages in your test results. Squish does not offer a ready-made function for different log-levels, but you can easily create this functionality.

We’ll use the test.log(message, detail) Squish API which allows custom log messages. To add the desired functionality, we need to override the test.log function and use the detail parameter for finding out which log level is set for each test.log call. Using Python, this would like something like the following:

def overrideTestLog():
     # Store a reference to the original function
     test.originalLog = test.log
     test.log = myTestLog

We wrote an earlier blog about overriding Squish API functions. Take a look here for more information.

In order to execute this code, we need to define the myTestLog function which does the main job:

def myTestLog(message, detail=None):
     if detail is None:
         # Just to find log statements which haven't set the log 
         # level correctly yet
         test.warning("log statement --" + message + "-- has no detail")
     elif detail <= logLevel:
         test.originalLog(message)

The above script code does the following:

  • In case there is no detail set, a warning message will be added in the test results.
  • In case your set level is equivalent or lower, the message will be printed in the test results.

Note: By changing "<=" to "==", you can choose to issue a log message only for that particular level.

An example test case shows the complete code:

def main():
    global logLevel
    logLevel = 2
    test.log("Loglevel is set to: " + str(logLevel))
    
    overrideTestLog()
    test.log("Loglevel 3 Test", 3)
    test.log("Log message without detail")
    test.log("Loglevel 2 Test", 2)
    test.log("Loglevel 1 Test", 1)
    test.log("Loglevel without detail")

def myTestLog(message, detail=None):
    if detail is None:
        # Just to find log statements which haven't set the log 
        # level correctly yet
        test.warning("log statement --" + message + "-- has no detail")
    elif detail <= logLevel:
        test.originalLog(message)

        
def overrideTestLog():
    # Store a reference to the original function
    test.originalLog = test.log
    test.log = myTestLog

The value of the global variable logLevel should be gathered from your test data.

You can run this script without any additional settings. Simply create a new test case, and copy the code into the freshly created test case. Doing so, your reporting will look like this:

Example Squish test result and log output when setting different log levels.
Christian Franke started as a support engineer in 2017 right after finishing his Bachelor's degree. Since then, he's taken over sales-relevant tasks as well as conducting trainings and offering consulting from time to time.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*