python unit test frame work

Cameron Simpson cs at zip.com.au
Thu Dec 10 19:40:21 EST 2015


On 11Dec2015 04:05, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>On Dec 11, 2015 3:57 AM, "Ganesh Pal" <ganesh1pal at gmail.com> wrote:
>> > Drop the habit to sprinkle sys.exit() all over the place. A well-behaved
>> > application has one exit point, at the end of the main module.
>>
>I was using sys.exit() as the means to stop the  execution or terminate the
>program. I can't think of an alternative for my case :
>I have multiple checks if I don't meet them continuing with the main
>program doesn't make sense

First, as Ben remarks, if one test _depends_ on an earlier one then it isn't a 
real unit test.

On the other hand, if you simply have some simple tests followed by more 
complex tests (I have several like this) you have two facilities to help you.  

Firstly, I have observed that unittest tends to run tests in lexical order, so 
I tend to name the test methods like this:

  def test00minimalist(self):
  def test01minimalPlusOneThing(self):
  ...
  def test20somethingComplex(self):

and on the command line when you are hand testing things:

  python -m cs.venti.tcp_tests -v -f

Of course, substitute your module name. The -v recites the test names and 
docstring first line as Peter has mentioned. The -f causes the etst suit to 
fail on the first test failure, and does not run the later tests. That seems to 
cover what you want here: abort on the simplest failing test.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list