How to spread django unit tests over multiple files?

后端 未结 10 1918
后悔当初
后悔当初 2020-12-07 11:46
  • I have a python-django application
  • I\'m using the unit testing framework
  • The tests are arranged in the file \"tests.py\" in the module directory
相关标签:
10条回答
  • 2020-12-07 12:37

    Just make your directory structure like this:

    myapp/
        __init__.py
        tests/
            __init__.py
            test_one.py
            test_two.py
            ...
        ...
    

    And python manage.py test myapp will work as expected.

    0 讨论(0)
  • 2020-12-07 12:37

    I have two files. One is tests.py and another is test_api.py. I can run these individually as below.

       manage.py test companies.tests
       manage.py test companies.test_api
    

    Refer @osa's response about file naming convention.

    0 讨论(0)
  • 2020-12-07 12:41

    The behavior has changed in Django 1.6, so there is no longer a need to create a package. Just name your files test*.py.

    From Django 1.7 documentation

    When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite.

    From Django 1.6 documentation,

    Test discovery is based on the unittest module’s built-in test discovery. By default, this will discover tests in any file named “test*.py” under the current working directory.

    Previous behavior, from Django 1.5 documentation:

    When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in models.py and tests.py, automatically build a test suite out of those test cases, and run that suite.

    There is a second way to define the test suite for a module: if you define a function called suite() in either models.py or tests.py, the Django test runner will use that function to construct the test suite for that module. This follows the suggested organization for unit tests. See the Python documentation for more details on how to construct a complex test suite.

    0 讨论(0)
  • 2020-12-07 12:43

    The answer as stated by Tomasz is correct. However, it can become tedious to ensure that the imports in __init__.py match your file structure.

    To automatically detect all tests in the folder you can add this in __init__.py:

    import unittest
    
    def suite():   
        return unittest.TestLoader().discover("appname.tests", pattern="*.py")
    

    This will allow you to run ./manage.py test appname but won't handle running specific tests. To do that you can use this code (also in __init__.py):

    import pkgutil
    import unittest
    
    for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
        module = loader.find_module(module_name).load_module(module_name)
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, unittest.case.TestCase):
                exec ('%s = obj' % obj.__name__)
    

    Now you can run all your tests via manage.py test app or specific ones via manage.py test app.TestApples

    0 讨论(0)
提交回复
热议问题