Вопрос

I'm using Django 1.6 and currently i'm organizing my functional and unit tests in their own separate modules like so:

/functional_tests/test*.py

/unit_tests/test*.py

My question is, where does the distinction between creating multiple classes in a single file vs. creating multiple files with a single class in each lie? Is there a standard best practice?

Thanks

Это было полезно?

Решение

I'll just describe my insight and share some links.

First of all, the good thing you are already doing is that you are separating functional and unit tests.

Also, I prefer to follow the "one test case per file" and "as few assertions per test method as possible" rules.

But, sometimes, you want to test, for example, different "modes" of one view, let's say: change password view can be called if the user just wants to change a password and if the user forgets it and changes the password using temporary one. In this case, it's probably a good idea to split the view test into two TestCases, each for different mode. In this situation, you can either put both test cases into the same file, or create a package with two separate files. It usually depends on the test complexity, size and readability.

And, also, actually, Zen of Python principles could help:

  • Flat is better than nested. (probably don't have multiple test cases per file)
  • Readability counts. (if a test case is huge, probably you need to split it into logical parts)

Also some links on the subject:

Also, it's worth looking at how django itself organizes tests.

Hope that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top