Django Test Runner

Question or problem about Python programming:

Getting a Django Application to 100% Test Coverage. Code coverage is a simple tool for checking which lines of your application code are run by your test suite. 100% coverage is a laudable goal, as it means every line is run at least once. Coverage.py is the Python tool for measuring code coverage.

I am new to both Python and Django and I’m learning by creating a diet management site but I’ve been completely defeated by getting my unit tests to run. All the docs and blogs I’ve found say that as long as it’s discoverable from tests.py, tests.py is in the same folder as models.py and your test class subclasses TestCase, it should all get picked up automatically. This isn’t working for me, when I run manage.py test it doesn’t find any tests.

Testing in Django. Automated testing is an extremely useful bug-killing tool for the modern Web developer. You can use a collection of tests – a test suite – to solve, or avoid, a number of problems: When you’re writing new code, you can use tests to validate your code works as expected. When you’re refactoring or modifying old code. If this checkbox is selected, Django test will run with the specified custom settings, rather than with the default ones. Specify the fully qualified name of the file that contains Django settings. You can either type it manually, in the text field to the right, or click the browse button, and select one in the dialog that opens. The first thing to do is install django-nose using pip: Then make these additions to your project’s settings.py: Here, we’re setting a couple of command-line arguments to be included every time we run python manage.py test. The -with-coverage option says we want a coverage report, and the -cover-package option lists all of the modules we. The best base class for most tests is django.test.TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction. The class also owns a test Client that you can use to simulate a.

I started with all my tests in their own package but have simplified it down to all tests just being in my tests.py file. The current tests.py looks like:

I have tried it by subclassing the Django TestCase class as well but this didn’t work either. I’m using Django 1.1.1, Python 2.6 and I’m running Snow Leopard.

I’m sure I am missing something very basic and obvious but I just can’t work out what. Any ideas?

Edit: Just a quick update after a comment

To get the tests to run I am running manage.py test pyDietTracker

How to solve the problem:

Solution 1:

Django Test Runner Pytest

I had the same issue but my problem was different.

Test

I was getting Ran 0 tests, as OP.

But it turns out the test methods inside your test class must start with keyword test to run.

Example:

Also the files with your tests have to start with test keyword.

Solution 2:

If you’re using a yourapp/tests package/style for unittests, make sure there’s a __init__.py in there (since that’s what makes it a Python module!).

Pycharm Django Test Runner

Solution 3:

I can run test for specific apps e.g.

but when I run

0 tests was found

Figure out I need to run this in the same directory as manage.py

so the solution would be, cd to project directory and run

Solution 4:

In my case, the app folder itself was missing an __init__.py. This results in the behaviour that the test will be run with python manage.py test project.app_name but not with python manage.py test.

Solution 5:

This also happens if you have a syntax error in your tests.py.

Solution 6:

Worked it out.

It turns out I had done django-admin.py startproject pyDietTracker but not python manage.py startapp myApp. After going back and doing this, it did work as documented. It would appear I have a lot to learn about reading and the difference between a site and an app in Django.

Thank you for your help S.Lott and Emil Stenström. I wish I could accept both your answers because they are both helped alot.

Most important lesson Tests only work at the app level not the site level

Django Run Tests

Solution 7:

In my case, I typed def instead of class. Instead of

I had

Solution 8:

This may also happen when you are using a tests module instead of a tests.py. In this case you need to import all the test classes into the __init__.py of your tests module, e.g.

In your __init__.py you now need to import the somemodule like this:

Solution 9:

Here’s another one that I’ve just had: Check your test files are not executable. My virtualbox auto-mounted them as executable so the test discover missed them completely. I had to add them into the relevant __init__.py files before someone told me what the issue was as a work around, but now they are removed, and non-executable and everything _just_works.

Solution 10:

Django Test Case

I had this happen when I had a test.py file, and a test/ subdirectory, in the same Django app directory. I guess I’m confusing python or the test runner whether I’m looking for a test module (in test.py) or a test package (in test/ subdir).

Django Test Suite Runner

Hope this helps!