Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python TDD directory structure

Is there a particular directory structure used for TDD in Python?

Tutorials talk about the content of the tests, but not where to place them

From poking around Python Koans, suspect its something like:

/project/main_program.py         # This has main method, starts program
/project/classes/<many classes>.py
/project/main_test.py            # This simply directs unittest onto tests, can use parameters fed to it to customise tests for environment
/project/tests/<many tests>.py

# to run tests, type "python -m unittest main_test.py" (into a terminal)
# to run program, type "python main_program.py"

Am I doing this right? Is there a good guide which teaches the directory hierarchy for TDD? I heard that having mixed files of code and tests is bad.

References:

  • Are there any good online tutorials to TDD for an experienced programmer who is new to testing? # A coding dojo? hmm... Perhaps I'll start a coding dojo website...
  • http://onlamp.com/pub/a/python/2004/12/02/tdd_pyunit.html #Shows mixed files
  • http://www.youtube.com/watch?v=sD6qzJNQEpE #As great as pyTDDmon looks, I'd like to understand the basics first =) also thats a mixed file
  • http://www.slideshare.net/Skud/test-driven-development-tutorial #explains "design test implement test repeat" only..
  • http://blog.cerris.com/category/django-tdd/ #Still no help...
  • http://docs.python.org/library/unittest.html
like image 617
xxjjnn Avatar asked Mar 25 '12 11:03

xxjjnn


People also ask

Where are Python unit tests stored?

Tests are defined as functions prefixed with test_ and contain one or more statements that assert code produces an expected result or raises a particular error. Tests are put in files of the form test_*. py or *_test.py , and are usually placed in a directory called tests/ in a package's root.

How do I run all Python unit tests in a directory?

Run all Python tests in a directoryFrom the context menu, select the corresponding run command. If the directory contains tests that belong to the different testing frameworks, select the configuration to be used. For example, select Run 'All tests in: <directory name>' Run pytest in <directory name>'.


2 Answers

Based on your project, Whatever style lets you

  • Seperate implementation code from testing code
  • Create new tests easily
  • Run all tests in one operation (e.g. for regression testing)

The python koans/etc are just guidelines. In the end you want to uphold DRY with your unittests and be able to test easily, maintainably and intuitively. In the end it is up to you to decide your folder structure.

I feel like you are focusing too much on satisfying convention instead of satisfying your project.

like image 157
Preet Kukreti Avatar answered Sep 21 '22 15:09

Preet Kukreti


There are two basic options: in a top-level "test" (or "tests") directory, or in "test" directories within your package at every level. The former has the advantage of making it easy to have both unit tests and other tests consistently. The latter has the advantage of making it easy to run your tests against the installed version of the code, and is recommended by this blog post, which describes the basic structure that works well for Python projects.

At the end of the day, the important thing is to make them easy to find and run.

like image 37
Mike Graham Avatar answered Sep 21 '22 15:09

Mike Graham