control-port-filter add_onion unit test


ID: 566
PHID: PHID-TASK-x6xcuqw3phytdobgcmes
Author: Patrick
Status at Migration Time: invalid
Priority at Migration Time: Normal


! 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 25.3. unittest — Unit testing framework — Python 2.7.18 documentation
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:

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.



2016-11-12 18:38:00 UTC