|
| 1 | +Testing your plugins with unittest |
| 2 | +================================== |
| 3 | + |
| 4 | +This guide explains how to test your Errbot plugins using the built-in testing framework. Errbot provides a powerful testing backend called ``FullStackTest`` that allows you to write unit tests for your plugins in a familiar unittest style. |
| 5 | + |
| 6 | +Basic Test Setup |
| 7 | +-------------- |
| 8 | + |
| 9 | +To test your plugin, create a test file (e.g., `test_myplugin.py`) in your plugin's directory. Here's a basic example: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + import unittest |
| 14 | + from pathlib import Path |
| 15 | +
|
| 16 | + from errbot.backends.test import FullStackTest |
| 17 | +
|
| 18 | + path = str(Path(__file__).resolve().parent) |
| 19 | + extra_plugin_dir = path |
| 20 | +
|
| 21 | +
|
| 22 | + class TestMyPlugin(FullStackTest): |
| 23 | + def setUp(self): |
| 24 | + super().setUp(extra_plugin_dir=extra_plugin_dir) |
| 25 | +
|
| 26 | + def test_my_command(self): |
| 27 | + # Simulate a user sending a command |
| 28 | + self.push_message('!hello') |
| 29 | + self.assertIn('Hello!', self.pop_message()) |
| 30 | +
|
| 31 | +Running Tests |
| 32 | +------------ |
| 33 | + |
| 34 | +You can run your tests using Python's unittest framework: |
| 35 | + |
| 36 | +.. code-block:: bash |
| 37 | +
|
| 38 | + python -m unittest test_myplugin.py |
| 39 | +
|
| 40 | +Test Methods |
| 41 | +----------- |
| 42 | + |
| 43 | +FullStackTest provides several methods to help test your plugin's behavior: |
| 44 | + |
| 45 | +1. **Message Handling**: |
| 46 | + - ``push_message(command)``: Simulate a user sending a command |
| 47 | + - ``pop_message(timeout=5, block=True)``: Get the bot's response |
| 48 | + - ``assertInCommand(command, response, timeout=5)``: Assert a command returns expected output |
| 49 | + - ``assertCommandFound(command, timeout=5)``: Assert a command exists |
| 50 | + |
| 51 | +2. **Room Operations**: |
| 52 | + - ``push_presence(presence)``: Simulate presence changes |
| 53 | + - Test room joining/leaving |
| 54 | + - Test room topic changes |
| 55 | + |
| 56 | +3. **Plugin Management**: |
| 57 | + - ``inject_mocks(plugin_name, mock_dict)``: Inject mock objects into a plugin |
| 58 | + - Test plugin configuration |
| 59 | + - Test plugin dependencies |
| 60 | + |
| 61 | +Example Test Cases |
| 62 | +---------------- |
| 63 | + |
| 64 | +Here are some example test cases showing different testing scenarios: |
| 65 | + |
| 66 | +1. **Basic Command Testing**: |
| 67 | + |
| 68 | + .. code-block:: python |
| 69 | +
|
| 70 | + def test_basic_command(self): |
| 71 | + self.push_message('!echo test') |
| 72 | + self.assertIn('test', self.pop_message()) |
| 73 | +
|
| 74 | +2. **Command with Arguments**: |
| 75 | + |
| 76 | + .. code-block:: python |
| 77 | +
|
| 78 | + def test_command_with_args(self): |
| 79 | + self.push_message('!repeat test 3') |
| 80 | + response = self.pop_message() |
| 81 | + self.assertIn('testtesttest', response) |
| 82 | +
|
| 83 | +3. **Error Handling**: |
| 84 | + |
| 85 | + .. code-block:: python |
| 86 | +
|
| 87 | + def test_error_handling(self): |
| 88 | + self.push_message('!nonexistent') |
| 89 | + response = self.pop_message() |
| 90 | + self.assertIn('Command not found', response) |
| 91 | +
|
| 92 | +4. **Mocking Dependencies**: |
| 93 | + |
| 94 | + .. code-block:: python |
| 95 | +
|
| 96 | + def test_with_mocks(self): |
| 97 | + # Create mock objects |
| 98 | + mock_dict = { |
| 99 | + 'external_api': MockExternalAPI() |
| 100 | + } |
| 101 | + self.inject_mocks('MyPlugin', mock_dict) |
| 102 | +
|
| 103 | + # Test plugin behavior with mocks |
| 104 | + self.push_message('!api_test') |
| 105 | + self.assertIn('Mock response', self.pop_message()) |
| 106 | +
|
| 107 | +Best Practices |
| 108 | +------------- |
| 109 | + |
| 110 | +1. **Test Isolation**: Each test should be independent and not rely on the state from other tests. |
| 111 | + |
| 112 | +2. **Setup and Teardown**: Use ``setUp()`` to initialize your test environment and ``tearDown()`` to clean up. |
| 113 | + |
| 114 | +3. **Timeout Handling**: Always specify appropriate timeouts for message operations to avoid hanging tests. |
| 115 | + |
| 116 | +4. **Error Cases**: Include tests for error conditions and edge cases. |
| 117 | + |
| 118 | +5. **Documentation**: Document your test cases to explain what they're testing and why. |
| 119 | + |
| 120 | +Complete Example |
| 121 | +-------------- |
| 122 | + |
| 123 | +Here's a complete example of a test suite for a plugin: |
| 124 | + |
| 125 | +.. code-block:: python |
| 126 | +
|
| 127 | + import unittest |
| 128 | + from pathlib import Path |
| 129 | +
|
| 130 | + from errbot.backends.test import FullStackTest |
| 131 | +
|
| 132 | + path = str(Path(__file__).resolve().parent) |
| 133 | + extra_plugin_dir = path |
| 134 | +
|
| 135 | + class TestGreetingPlugin(FullStackTest): |
| 136 | + def setUp(self): |
| 137 | + super().setUp(extra_plugin_dir=extra_plugin_dir) |
| 138 | +
|
| 139 | + def test_basic_greeting(self): |
| 140 | + """Test the basic greeting command.""" |
| 141 | + self.push_message('!greet Alice') |
| 142 | + self.assertIn('Hello, Alice!', self.pop_message()) |
| 143 | +
|
| 144 | + def test_greeting_with_options(self): |
| 145 | + """Test greeting with different options.""" |
| 146 | + # Test with count |
| 147 | + self.push_message('!greet Bob --count 2') |
| 148 | + response = self.pop_message() |
| 149 | + self.assertIn('Hello, Bob!Hello, Bob!', response) |
| 150 | +
|
| 151 | + # Test with shout |
| 152 | + self.push_message('!greet Charlie --shout') |
| 153 | + self.assertIn('HELLO, CHARLIE!', self.pop_message()) |
| 154 | +
|
| 155 | + def test_error_handling(self): |
| 156 | + """Test how the plugin handles errors.""" |
| 157 | + # Test missing name |
| 158 | + self.push_message('!greet') |
| 159 | + self.assertIn('Please provide a name', self.pop_message()) |
| 160 | +
|
| 161 | + # Test invalid count |
| 162 | + self.push_message('!greet Eve --count abc') |
| 163 | + self.assertIn('must be an integer', self.pop_message()) |
| 164 | +
|
| 165 | +
|
| 166 | + if __name__ == '__main__': |
| 167 | + unittest.main() |
0 commit comments