Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the normal structure of a Python open source project and what's the preferred way of running the tests? [closed]

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
like image 753
Geo Avatar asked Jun 11 '11 23:06

Geo


People also ask

What is Python directory structure?

Your app is stored as a directory structure that you can clone using Git. Each Package, Module and Form consists of files and directories within this structure.

How do I run a Python test?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.


1 Answers

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).

like image 66
Yannick Versley Avatar answered Sep 21 '22 11:09

Yannick Versley