Maybe I should test tests ?

Maybe I should test tests ?

Published Oct. 19, 2015 in Development - Last update on Oct. 19, 2015.

Every one has already test a big sized app realizes he must write his own test framework for make clear tests with easy reading and usage. For example, Django has its own test framework, inherited from unittest, for ease tests for models, views, mails and more. Even with Django I sometimes need to create modules, classes or decorators  which I use in 75% of my tests. And arrives the day when you ask yourself "Should I test my tests ?". My answer "Yes"

Docstring and Doctest

That's my solution ! Little reminder:

The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.

Documentation inside code is really helpful for big docstring-consumers like I am. It doesn't upset me to have giant documentation at top of module with real example inside. Same thing for classes and methods, a fast copy/paste after look at to the help is really helpful.

Demonstration by example

Anyway, here I want to ensure my test framework ever works. Instead of write more Python module for test tests (and maybe later test tests' tests), I simply grealty documentate my test utilities and include inside doctests. In the below example, I have an helper for use a JukeBox in my tests:

class JukeBoxHelper(object):
    """
    A superb class which allow to launch music during your tests.
    Very useful with very long testcases.

    >>> juke = JukeBoxHelper()
    >>> juke.start_music()
    'Trololo'
    >>> juke.stop_music()
    """
    def start_music(self):
        return 'Trololo'

    def stop_music(self):
        pass

Not more, not less, I simply put my manual tests made from command-line interpreter (with PS1 included haha).

Launch doctest can be made following command:

python -m doctest -v myutils.py

Or if you want to inlude in global unit tests suite you can adpat the following snippet:

import unittest
import doctest
import my_module_with_doctests

def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite(my_module_with_doctests))
    return tests

Comments

Post your comment

Comment as . Log out.