Python TDD directory structure

2024/9/29 7:22:18

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
Answer

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.

https://en.xdnf.cn/q/71235.html

Related Q&A

Pillow was built without XCB support

Im working on a program that uses ImageGrab in Pillow. I am getting the error mentioned in the title. I notice in the documentation that it says the generic pip install Pillow doesnt come with libxcb. …

Set equal aspect in plot with colorbar

I need to generate a plot with equal aspect in both axis and a colorbar to the right. Ive tried setting aspect=auto, aspect=1, and aspect=equal with no good results. See below for examples and the MWE.…

How to emit dataChanged in PyQt5

The code below breaks on self.emit line. It works fine in PyQt4. How to fix this code so it works in PyQt5?from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignalclass …

django: gettext and coercing to unicode

I have following code in my django application.class Status(object):def __init__(self, id, desc):self.id = idself.desc = descdef __unicode__(self):return self.descSTATUS = Status(0, _(u"Some text&…

Telegram bot api keyboard

I have problem with Telegram Bot Api and with "ReplyKeyboard". Im using Python 2.7 and I send post request:TelegramAPI.post(TELEGRAM_URL + "sendMessage", data=dict(chat_id=CHAT_ID, …

Use of torch.stack()

t1 = torch.tensor([1,2,3]) t2 = torch.tensor([4,5,6]) t3 = torch.tensor([7,8,9])torch.stack((t1,t2,t3),dim=1)When implementing the torch.stack(), I cant understand how stacking is done for different di…

Is it possible to sort a list with reduce?

I was given this as an exercise. I could of course sort a list by using sorted() or other ways from Python Standard Library, but I cant in this case. I think Im only supposed to use reduce().from funct…

Flask-WTF set time limit on CSRF token

Im currently using Flask-WTF v0.13.1, i have a few forms on my website, all created including the CSRF token.For some reasons i have to set a different expiration on each form, so far i could set manua…

Extracting Intermediate layer outputs of a CNN in PyTorch

I am using a Resnet18 model. ResNet((conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats…

In Tensorflow, how to unravel the flattened indices obtained by tf.nn.max_pool_with_argmax?

I meet a problem: After I use the tf.nn.max_pool_with_argmax, I obtain the indices i.e. argmax: A Tensor of type Targmax. 4-D. The flattened indices of the max values chosen for each output.How to unr…