Information
ID: 566
PHID: PHID-TASK-x6xcuqw3phytdobgcmes
Author: Patrick
Status at Migration Time: invalid
Priority at Migration Time: Normal
Description
! 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 fromunittest.TestCase
, then add method(s) namedtest_something
and useself.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
andsend_response
instead ofprepare_and_send_response
. But even in the later case, you can create wrapper class (just for testing purpose) to overrideself.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.
Comments
Patrick
2016-11-12 18:38:00 UTC