|
| 1 | +=================== |
| 2 | +where to put tests? |
| 3 | +=================== |
| 4 | + |
| 5 | +TL; DR |
| 6 | +====== |
| 7 | + |
| 8 | +Too Long; Didn't Read: |
| 9 | + |
| 10 | +If your package and test code are small and self contained, put the tests in with the package. |
| 11 | + |
| 12 | +IF the tests are large or complex, or require reading/writting files, or significatn samepl data, put yoru tests outside the package. |
| 13 | + |
| 14 | +Test system recommendations |
| 15 | +---------------------------- |
| 16 | + |
| 17 | +``pytest`` has a bit of discussion of the issue here: |
| 18 | + |
| 19 | +https://pytest.org/latest/goodpractises.html |
| 20 | + |
| 21 | +I need to add links for nose and unittest.... |
| 22 | + |
| 23 | + |
| 24 | +Two Options |
| 25 | +----------- |
| 26 | + |
| 27 | +In Python packaging, there seems to be no consensus on where you should put your test suite. |
| 28 | + |
| 29 | + |
| 30 | +There are essentially two recommended approaches: |
| 31 | + |
| 32 | +1) Put all your test code outside the package, and have it all designed to import from an installed base:: |
| 33 | + |
| 34 | + from my_package import the_module_to_test |
| 35 | + |
| 36 | +to do that, you need to install your package under development in "develop" mode:: |
| 37 | + |
| 38 | + python setup.py develop |
| 39 | + |
| 40 | +or |
| 41 | + |
| 42 | + pip install -r ./ |
| 43 | + |
| 44 | +That means that you do need a setup.py -- though it can be very minimal. See: |
| 45 | + |
| 46 | +https://packaging.python.org/en/latest/ |
| 47 | + |
| 48 | +for recommendations. |
| 49 | + |
| 50 | +2) The other options is to put your test code in a sub-package inside your package. In this case, it should be inside your package, and *be* a package itself (i.e. have an ``__init__.py``):: |
| 51 | + |
| 52 | + my_package |
| 53 | + __init__.py |
| 54 | + module_1.py |
| 55 | + module_2.py |
| 56 | + test |
| 57 | + __init_.py |
| 58 | + test_1.py |
| 59 | + test_2.py |
| 60 | + |
| 61 | +Self contained |
| 62 | +-------------- |
| 63 | + |
| 64 | +The advantage of keeping test code self-contained is that you can have a large suite of tests with sample data and who knows what, and it won't bloat and complicate the installed package (and test code can write to the test dirs, etc..... Also, you can then run the test suite against an installed version that may not be exactly the same as the current live code. |
| 65 | + |
| 66 | +Sub-package |
| 67 | +----------- |
| 68 | + |
| 69 | +The advantage of test being a sub-package is that your test code gets installed with the package, so users (including yourself, if you are deploying the code) can install the package, and run the test to make sure the install all went well. You can also have the tests use relative imports, so you can run it all without installing (though with develop mode I don't think that is much of an advantage) |
0 commit comments