In T448#10487, @marmarek wrote:In T448#10482, @Patrick wrote:
I guess we don't get around unit testing now. Do you have any examples where I can define function names, inputs and expected outputs?
Take a look at https://docs.python.org/2/library/unittest.html
Generally, add a new class inheriting from unittest.TestCase, then add method(s) named test_something and use self.assertEqual(expected_result, actual_result) (or other method - see linked documentation). Then call it: python -m unittest path/to/file
Some example: https://github.com/marmarek/qubes-core-admin/blob/core3-firewall/qubes/tests/firewall.py
It's easier to write unit tests when code is sufficiently divided to functions: for example have separate prepare_response and send_response instead of prepare_and_send_response. But even in the later case, you can create wrapper class (just for testing purpose) to override self.send with something that will just save the data for later checking.
It is common practice to have tests in separate file, but that is possible only when the actual code is installed as a python module, not directly in executable. Not sure if worth changing it here, probably not.