Search code examples
pythonunit-testingcontinuous-integrationfunctional-testing

How to designate Python unit tests as having database dependency?


I am working on a project that has many "unit tests" that have hard dependencies that need to interact with the database and other APIs. The tests are a valuable and useful resource to our team, but they just cannot be ran independently, without relying on the functionality of other services within the test environment. Personally I would call these "functional tests", but this is just the semantics already established within our team.

The problem is, now that we are beginning to introduce more pure unit tests into our code, we have a medley of tests that do or do not have external dependencies. These tests can be ran immediately after checking out code with no requirement to install or configure other tools. They can also be ran in a continuous integration environment like jenkins.

So my question is, how I can denote which is which for a cleaner separation? Is there an existing decorator within unit testing library?


Solution

  • You can define which test should be skipped with the skipIf decorator. In combinations with setting an environmental variable you can skip tests in some environments. An example:

    from unittest import skipIf
    
    class MyTest(Testcase):
    
        @skipIf(os.environ.get('RUNON') == 'jenkins', 'Does not run in Jenkins')
        def test_my_code(self):
            ...