I'm just learning django testing. When I use 'Coverage' module to check which parts of my code is tested, it considers almost all of class-based-views as tested while I have commented out all of my tests. I investigated this problem and found that in urls.py I have this line:
from . import views
It seems importing views.py file or just a class from views.py make all classes in views.py run once e.g. the print line in this code will be executed:
class AuthorDelete(PermissionRequiredMixin, DeleteView):
model = Author
success_url = reverse_lazy('authors')
permission_required = 'catalog.delete_author'
print("----- AuthorDelete -------")
And 'Coverage' will mark them as tested. But obviously this class is not tested at all.
How can I get real and exact test coverage report?
Should I use other tools rather than 'Coverage'?
Thanks in advance
coverage.py
can only tell what gets executed, not why it was executed. The body of theclass
statement is executed on import whether or not any other code you test uses the class.coverage.py
doesn't distinguish between "tests" and "code being tested". It just notes whether a line of code is executed or not. If you run your tests and a line is not executed, you can be assured it wasn't tested. But if a line is executed, you can't be sure whether the result of that code was subject to a test.