0

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

4
  • 1
    coverage.py can only tell what gets executed, not why it was executed. The body of the class statement is executed on import whether or not any other code you test uses the class.
    – chepner
    Commented Jun 4 at 16:44
  • @chapner That's correct, but how can I use 'coverage' for checking which parts of code is really tested?
    – mohsenof
    Commented Jun 5 at 5:19
  • You can't. 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.
    – chepner
    Commented Jun 5 at 11:53
  • @chepner Thank you. Is there any other tools that can be relied on for checking which parts of code is really tested?
    – mohsenof
    Commented Jun 6 at 12:39

0

Browse other questions tagged or ask your own question.