5

Short Question
What is the best way to get data into a Python unittest case ?

Background
My project is using Python's unittest module as an automated way execute a series of tests that will need to run on many of the same type of boards. So far this is a good fit to what the unittest module was designed for; the twist is that each test case needs to know run specific information to store in a Django database.

The data that needs to be passed in includes a serial number, who tested the board, the date, and other things of this nature. It is worth noting that the order in which the boards will be tested is chosen by a human that pulls board X from a box, so predicting the serial number is not possible.

Thoughts
Currently, I am passing the required data to and from the test cases via pickle. This method works fine in small testing, but my concern is reading and writing to the same file 100k + times gives lots of room for data corruption (+ it's not that fast). I wrote an answer to a SO Question that redirects the stdin in a way that I think might work well for this application as well.

The next step is going to be wrap a GUI around these tests. A personal goal would be to have the ability to run the tests via command line then have the GUI call the same command line functions. For this reason I am leaning towards moving to the redirected stdin.

System / Deployment Information
Required OS Support: Windows XP and Windows 7
Ideal OS Support: Mac OS X and Linux
Python Version: 2.7

Any thoughts or comments would be greatly appreciated.

Community
  • 1
  • 1
Adam Lewis
  • 7,017
  • 7
  • 44
  • 62
  • regarding reading the same file 100k+ times: I think that OS will read the file once and then will try keeping it in the memory, thus eliminating the reading time and disk wearing-out. – Boris Gorelik Aug 08 '11 at 10:51
  • @bgbg: I didn't look at it this way, but I would tend to agree with your assessment. – Adam Lewis Aug 09 '11 at 16:40

1 Answers1

2

I have created unittests which test against a third party service (Zoho CRM). To test the service API, you need to store username and password crendetials.

Since this is premium service and you are creating open source software, naturally you cannot hardcode your login credentials to the source code itself.

So I ended up using environment variables - work quite well:

Here is the example:

https://github.com/miohtama/mfabrik.zoho/blob/master/mfabrik/zoho/tests.py

As a bigger problem I think trying to enforce unit tests module to do something it was not supposed to do in the first place is not a good idea. Maybe you should try to write your own unit test runner which would do necessary preparements (pulling info, storing results) somewhere.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Using environment variables is a great idea. They actually address the concern of writing to the file system and removes the Python version dependency. – Adam Lewis May 01 '11 at 02:53
  • Ugh, Pressed enter to soon: Regarding rewriting the unittest runner, I'm not sure I completely agree with the statement. I don't see it as a problem, but as a reuse of a extensively tested module. In addition, the ability to use nose and other plugins has become very useful as well. Thanks again for the tip on the environment variables! – Adam Lewis May 01 '11 at 03:03