croach / Flask-Fixtures

A simple library for adding database fixtures for unit tests using nothing but JSON or YAML.
MIT License
63 stars 30 forks source link

Add support for standalone test functions #11

Open croach opened 9 years ago

croach commented 9 years ago

The py.test and nose libraries support standalone test functions (the unittest standard library does not). I'd like to add support for these standalone functions to the Flask-Fixtures library.

If we wrap each function with a class that inherits from the FixturesMixin (a simplified example listed below) we'll be able to use standalone functions even when using the unittest library. This may be a good or bad thing since it means that decorated functions will work with unittest, but regular functions without the decorator will not, which may be a bit confusing to anyone writing tests. The alternative is just to wrap the test function in another function and the py.test and nose libraries will work properly, and unittest will continue to not work with any standalone functions.

import unittest
from functools import wraps

def bar(fn):
    @wraps(fn)
    def _test_fn(self):
        print
        print "do something before..."
        fn()
        print "do something after..."

    return type('Test_' + fn.__name__, (unittest.TestCase,), {
        fn.__name__: _test_fn
    })

@bar
def test():
    assert True

@bar
def test2():
    assert True