0
0
0LMER2015-04-07 10:08:02
Django
0LMER, 2015-04-07 10:08:02

Structure for storing unit tests in a Django (and not only) project?

Good day. I wondered about the coverage of code by unit tests. In python, I didn't really work with this before. Googling, I can not decide which option is correct (I doubt between the two).
Based on the n-th experience with Java, the following is practiced in toad projects:

src-|
  -main
    |-com.trolololo.foo
  -test
    |-com.trolololo.foo

That is, there is a directory with code, there is a parallel directory with tests, and the structure of the main directory is repeated in it.
Googling, I found such an example for python - the tests go into the package with the main code, into the tests.py file or the tests package . For Django, I saw examples when a tests file/package is created in each application at the root , and everything is written there. But I don’t quite understand how to be with a more complex structure.
So, there are 2 options:
1. By analogy with Java, but the structure is repeated within the application:
project
  |- app1
    |- services
      |- foo.py
      |- bar
        |- qwe.py
    |- models.py
    |- views.py
    |- tests
      |- test_models.py
      |- test_views.py
      |- services
          |- test_foo.py
          |- bar
            |- test_qwe.py

2. Create a tests.py file, or a tests package next to the code:
project
  |- app1
    |- services
      |- foo.py
      |- foo2.py
      |- bar
        |- qwe.py
        |- test_qwe.py
      |- tests
          |- test_foo.py
          |- test_foo2.py
    |- models.py
    |- views.py
    |- tests
      |- test_models.py
      |- test_views.py

Which option is correct?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2015-04-07
@0LMER

flat>>>nested
if you really, really need to nest, then you can split a large file into files and put them in a folder with the name of the directory and add an empty __init__.py

project
  |- app1
< вот такой фигни нет >
    |- services 
      |- foo.py
      |- bar
        |- qwe.py
</ вот такой фигни нет >
< для такой фигни есть commands >
    |-management
       |-commands
         |-__init__.py
         |-my_command.py
</ для такой фигни есть commands >
    |- models.py
    |- views.py
    |- tests
      |-__init__.py
      |- test_models.py
      |- test_models_supermodel.py
      |- test_views.py
      |-test_commands_my_command.py
  |-hugeapp
.....
    |- tests
       |-__init__.py
       |- test_models <<dir как питоновский пакет
          |-__init__.py           
             |- test_model1.py
             ....
             |- test_model999.py

V
Vadim Shandrinov, 2015-04-07
@suguby

Probably here the essence is in the reuse of dzhangi applications. If you plan to distribute applications (at least between your projects), then the tests file / package should be kept inside the application folder. Direct analogy with templates.
If the tests are functional, that is, for the behavior of the entire project and / or a bunch of applications, then naturally there is a package in the root of the project and there are test modules.
But in general, for me personally, the tests folders in each app clutter up the project when it is large - it is difficult to look at the entire project tree. Therefore, I often make one package tests at the root of the project and inside I approximately repeat the project tree. Flies from cutlets separately, so to speak.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question