Test: Was Logging called

While creating a function that uses to log useful information on the first run of Opsdroid the percentage of coverall dropped quite a bit. To counter that a test had to be created to assert if the was called or not.

Opsdroid uses a file named configuration.yml to keep track of all the configuration details. Upon the suggestion of Jacob(the creator and maintainer of the project) a welcome-message: true line was added to the configuration file in case the user wishes to hide the welcome message.

The Function

The welcome_message function is a very basic function that uses the to log a quick get started information about Opsdroid.

1def welcome_message(config):
2 """Add welcome message if set to true in configuration."""
3 try:
4 if config['welcome-message']:
5"=" * 40)
6"You can customise your opsdroid by modifying "
7 "your configuration.yaml")
8"Read more at: "
9 "")
10"Watch the Get Started Videos at: "
11 "")
12"Install Opsdroid Desktop at: "
13 "")
14"=" * 40)
15 except KeyError:
16 pass

The Test

1def test_welcome_message(self):
2 config = {"welcome-message": True}
3 with mock.patch('') as logmock:
4 opsdroid.welcome_message(config)
5 self.assertTrue(logmock.called)
  • We start the function by creating a dummy config file that will always return the welcome message.
  • On line 3 we patch the call from opsdroid__main__ (where the welcome_message function is located)
  • On line 4 we make a call to the welcome_message function using the dummy config file (which will always return the lines)
  • On line 5 logmock.called will return a boolean whether the mocked object has been called. Since we are sure that the dummy config file will always return True we do a simple assertTrue to see if was called.

(Update) An easier way

When I wrote this pos, back in September of 2017, it seemed that there was no way to test if a logging message was logged. That changed in Feb when the unittest framework was updated and introduced a brilliant new assert.

Now, you can quickly check if a message was logged with the assert self.assertLogs(logger, level). Let's say that your logger is called _Logger and you are currently logging the message "hello" with the following code"hello") for you to assert if the message was logged all you need to do is self.assertLogs('_Logger', 'info').

As you can see, three lines of code are just reduced into a single one (or two if you need to call the function), which is great because it makes the testing code much more readable.


0 Like 0 Comment

You might also like these