Building simple testing framework in Python

Every good product need to have a good testing coverage in order to insure that it works both in “happy scenarios” and in “bad scenarios” – inc. disruptions, limits and etc.

Beside unit tests, every module should be tested “end-to-end” as well. In this blog post I’ll demonstrate how to build a simple testing framework in python that will allow you to write tests, run them, get statistics and more. The important thing is making the tests runner scalable so  we can add tests without changing existing code.

Let’s start by defining a base class that all the tests should inherit from it. The base class will have some methods that can be overridden by the test implementation:

class TestBase(object):
    def setup(cls):

    def run_test(cls):
        raise Exception("Not Implemented")

    def tear_down(cls):

As you can see, the base class has 3 methods – one for preparing the environment before running the actual test, one for cleaning up the environment once the test is over and one for actual test implementation.

Now, let’s say all the tests will be in a directory called “tests”, each test will be defined as class that inherits from “TestBase” and probably will be in a different file. Now, what we need to do is write a function that can enumerate all the python files in this directory and find the classes that inherits from TestBase. This will give us the full list of the available tests and will allow us to dynamically create the test object and call it’s methods:

def get_available_tests():
    test_base = getattr(sys.modules["tests_common"], "TestBase")"Looking for available tests")
    files = os.listdir(os.path.join(os.getcwd(), "tests"))
    for file in files:
        if file.endswith(".py") and file != "":
            logger.debug("+ Found python file %s" % file)
            name = ".".join(file.split(".")[:-1])
            import_module("tests.%s" % name)

    tests = test_base.__subclasses__()"Loaded %d tests:" % len(tests))
    for test in tests:"+ %s" % test.__name__)
    return tests

Note that in order to use the “import_module” function, we need to make sure “tests” is a package. This is done simply by creating an empty “” file in “tests” directory. For more information, you can read about modules and packages in the following link.

Running a single test is done easily by calling the base class methods for each test:

def run_single_test(test):
    ok = True
    try:"Starting test: %s" % test.__name__)
        test.run_test()"Test execution completed successfully!")

    except Exception as e:
        logger.error("Test execution failed: %s" % e.message)
        logger.error("-" * 60)
        trace = traceback.format_exc().splitlines()
        for line in trace:
        logger.error("-" * 60)
        ok = False


    return ok

Note that we simply call the all 3 methods from the base class in the right order. in case there is a test failure, it should throw an exception. The exception will be catched, test execution will be marked as failure and we’ll print the stack trace for debugging purposes. After each test, we’ll call it’s tear_down method that shouldn’t throw any exception. If we want to protect ourselves, we can simply wrap it with try…except without doing any further actions.

Now, we can add some logic that allows us to choose which tests we want to run in the current execution. This can be done easily by getting a pattern and filtering the list of the tests using this pattern. For example, in case we have 3 tests called “TestCase1“, “MyShortTest” and “MyVeryComplicatedTest” – we can run the tests_runner with the following patterns: “*“, “TestCase1, MyShortTest“, “My*” and etc.

The filtering function is simple and looks like this:

def filter_tests(tests, pattern):
    filtered = list()
    if not(pattern):"No filtering pattern was supplied")
        return filtered

    patterns = pattern.split(",")

    for t in tests:
        for p in patterns:
            if fnmatch(t.__name__, p.strip()):
                continue"After filtering tests by given pattern, found %d tests to execute" % len(filtered))
    for t in filtered:"+ %s" % t.__name__)
    return filtered

We can get the pattern using command line arguments and provide more interesting options, for example – calculate tests duration, don’t stop test executions on failures and many more. A simple runner can look like this:

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-t", "--tests", dest="tests", help="Comma separated list of tests")
    parser.add_option("-c", "--continue-after-failure", dest="continue_after_failure", action="store_true", help="Continue to the next test in case of test failure")
    (options, args) = parser.parse_args()

    # Load available tests from "tests/" directory
    tests = get_available_tests()
    if len(tests) == 0:
        logger.error("No tests were found, aborting execution!")

    # In case no tests were supplied, assume the purpose was to list all available tests
    if not(options.tests):
        logger.warn("No tests were selected, exiting!")

    # Filter the tests we want to run
    to_run = filter_tests(tests, options.tests)

    # Run filtered tests
    passed_tests = 0
    failed_tests = 0
    total_duration = 0
    for test in to_run:
        # Run test and measure execution time
        start_time = time.time()
        ok = run_single_test(test)
        test_duration = time.time() - start_time"Test execution took %.2f seconds" % test_duration)

        total_duration += test_duration

        if ok:
            passed_tests += 1
            failed_tests += 1
            if not(options.continue_after_failure):
                logger.error("Discarding other tests due to failure")
                break"=" * 60)"Ran %d tests (%d passed, %d failed) in %.2f seconds" % (passed_tests + failed_tests, passed_tests, failed_tests, total_duration))
    sys.exit(1 if (failed_tests > 0) else 0)

You can find the full source code in the following link available for download with some example tests and usage output.

Bonus: If you are using a linux machine, this is a nice way for poor auto completion for test names, I use it where I have more than 100 tests and it’s really convenient.

Create a new bash file, for example let’s call it “” and source it from your ~/.bashrc file (don’t forget to add execution bit). The file should look like that:

#! /bin/bash

FILES="`<code>ls /<full-path>/tests/*.py`</code>"
TESTS="`grep -o -P '(?<=class ).*(?=\(TestBase\):)' ${FILES} | cut -d':' -f2`"

function tests {
    python /<full-path>/ -t $*

complete -W "${TESTS}" tests

In this script we simply list all the python files in the “tests” directory and grep the name of the classes that inherits from “TestBase”. The usage is simple: “tests “.

Hope I gave you some useful ideas for building your own testing framework.



3 thoughts on “Building simple testing framework in Python

    1. I’m not really familiar with the python unit testing framework, I’ve never used it. I guess it is almost the same, One of my purposes was to give some basic ideas, I chose python for implementation just because it’s easy 🙂
      This framework can be easily extended and modified for your own use-cases (for example I’ve added json file output for each suite run that contains the list of all the executed tests and it’s duration and I’m using it for load balancing tests between builders – so tests can run simultaneously).

      Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s