[Tutor] Modules and Test Suites

Lie Ryan lie.1296 at gmail.com
Tue Dec 29 22:40:32 CET 2009


On 12/30/2009 2:11 AM, Stephen Nelson-Smith wrote:
> I do quite a lot of programming in Ruby.  When I do so, my code tends
> to have the following layout:
>
> /path/to/src/my_project
>
> Inside my_project:
>
> lib/
> test/
> my_project.rb
>
> my_project.rb uses classes and helper methods in lib
>
> Inside test, I have a test suite that also uses classes and helper
> methods in ../lib
>
> This seems like a sensible way to keep tests and other code separate.
>
> In Python I don't know how to do this.... so I just have all my tests
> in the same place as the rest of the code.
>
> a) Is my above way a sensible and pythonic approach?

Yes, python does not enforce how you should structure your directory 
tree (though you will need to put several __init__.py files here and there).

> b) If so - how can I do it in Python?

/path/to/src/my_project
-> lib/
    ` __init__.py
    ` somelib.py
-> tests/
    ` __init__.py
    ` test_one.py
` my_project.py
` run_test.py

and you can reference your libraries and tests like so:

# my_project.py
from lib.somelib import SomeClass
import lib.somelib as sl

# run_test.py
import tests
tests.test_one.run_test()

> c) If not, is there a better way than having all the tests in the same
> place as the rest of the code?




More information about the Tutor mailing list