9

I wrote some code that I would like to share, and I would like to follow the best practices in creating/maintaining it's structure. I will host the code on BitBucket, and now I'm thinking about how I should organize it. Is this a good structure?

project_name/
    lib/
    test/
    README

So, this will have the source in lib, and the tests in test. Is this how it's done in Python projects? This is the structure I saw was the most used with Ruby projects. Also, when I run the unit tests, is it considered good practice to do it like this:

set PYTHONPATH=`pwd`/lib
python test/a_test.py
Geo
  • 93,257
  • 117
  • 344
  • 520
  • Well, 'pwd' is not cross-platform. Regarding PYTHONPATH in tests, see this question: http://stackoverflow.com/questions/1896918/running-unittest-with-typical-test-directory-structure – Todd Owen Jun 11 '11 at 23:53
  • @Todd Owen, yes, I know pwd's not cross-platform, but Python will consider it's value when running a script. – Geo Jun 12 '11 at 09:36

1 Answers1

1

The approach I've come to like is the following:

  • use distutils and create a setup.py file. (This is mostly useful when you have lots of extension classes). This will allow you to install the module system-wide or in a virtualenv directory.
  • If you want to have serious testing, but stay on the casual side of things, doctest is what you want, because it can double as "bare-bones" documentation (when you document the tests and include some commentary on what it's doing). You can either use doctest to use tests in the docstrings of your code or keep the tests in some separate .txt files.

You can integrate doctest by extending the setup command with an appropriate cmdclass=... entry in the setup.py file. See this example (CouchDB setup) for one solution that integrates testing in setup.py. (It uses separate files that have both the tests and the actual documentation, which is also a possibility).

Yannick Versley
  • 780
  • 3
  • 10