Commit 6ac44ee737908e07d63a7b101e32ad83401b4b10

Authored by Christian Herdtweck
1 parent 7c83ad38

Create unittest template file and howto

This should help avoiding some common mistakes when adding new unit tests
tests/howto_add_unittests.txt 0 → 100644
  1 +Howto: Add unittests
  2 +--------------------
  3 +
  4 +For helping python's unittest to discover your tests, do the
  5 +following:
  6 +
  7 +* create a subdirectory within oletools/tests/
  8 + - The directory name must be a valid python package name,
  9 + so must not include '-', for example
  10 + - e.g. oletools/tests/my_feature
  11 +
  12 +* Create a __init__.py inside that directory
  13 + - can be empty but must be there
  14 +
  15 +* Copy the unittest_template.py into your test directory
  16 +
  17 +* Rename your copy of the template to fit its purpose
  18 + - file name must start with 'test' and end with '.py'
  19 + - e.g. oletools/tests/my_feature/test_bla.py
  20 +
  21 +* Create python code inside that directory
  22 + - classes names must start with Test and must be subclasses
  23 + of Unittest.TestCase
  24 + - test functions inside your test cases must start with test_
  25 + - see unittest_template.py for examples
  26 +
  27 +* If your unit test requires test files, put them into a subdir
  28 + of oletools/tests/test-data with some name that clarifies what
  29 + tests it belongs to
  30 + - e.g. oletools/tests/test-data/my_feature/example.doc
  31 + - Do not add files with actual evil malware macros! Only harmless
  32 + test data!
  33 +
  34 +* Test that unittests work by running from the oletools base dir:
  35 + python -m unittest discover -v
  36 +
  37 +* Re-test with python2 and python3 (if possible)
tests/unittest_template.py 0 → 100644
  1 +""" Test my new feature
  2 +
  3 +Some more info if you want
  4 +
  5 +Should work with python2 and python3!
  6 +"""
  7 +
  8 +import unittest
  9 +
  10 +# if you need data from oletools/test-data/DIR/, uncomment these lines:
  11 +#from os.path import join, dirname, normpath
  12 +#Directory with test data, independent of current working directory
  13 +#DATA_DIR = normpath(join(dirname(__file__), '..', 'test-data', 'DIR'))
  14 +
  15 +
  16 +class TestMyFeature(unittest.TestCase):
  17 + """ Tests my cool new feature """
  18 +
  19 + def test_this(self):
  20 + """ check that this works """
  21 + pass # your code here
  22 +
  23 + def test_that(self):
  24 + """ check that that also works """
  25 + pass # your code here
  26 +
  27 + def helper_function(self, filename):
  28 + """ to be called from other test functions to avoid copy-and-paste
  29 +
  30 + this is not called by unittest directly, only from your functions """
  31 + pass # your code here
  32 + # e.g.: msodde.main(join(DATA_DIR, filename))
  33 +
  34 +
  35 +# just in case somebody calls this file as a script
  36 +if __name__ == '__main__':
  37 + unittest.main()