Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31560 > unrolled thread

Re: unittest for system testing

Started byMark Lawrence <breamoreboy@yahoo.co.uk>
First post2012-10-18 01:49 +0100
Last post2012-10-18 06:55 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: unittest for system testing Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-18 01:49 +0100
    Re: unittest for system testing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-18 06:55 +0000

#31560 — Re: unittest for system testing

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-10-18 01:49 +0100
SubjectRe: unittest for system testing
Message-ID<mailman.2378.1350521374.27098.python-list@python.org>
On 18/10/2012 01:22, Rita wrote:
> Hi,
>
> Currently, I use a shell script to test how my system behaves before I
> deploy an application. For instance, I check if fileA, fileB, and fileC
> exist and if they do I go and start up my application.
>
> This works great BUT
>
> I would like to use python and in particular unittest module to test my
> system and then deploy my app. I understand unittest is for functional
> testing but I think this too would be a case for it. Any thoughts? I am not
> looking for code in particular but just some ideas on how to use python
> better in situations like this.
>

Plenty of options here 
http://wiki.python.org/moin/PythonTestingToolsTaxonomy and an active 
mailing list that I read via gmane.comp.python.testing.general

-- 
Cheers.

Mark Lawrence.

[toc] | [next] | [standalone]


#31600

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-10-18 06:55 +0000
Message-ID<507fa7ce$0$11093$c3e8da3@news.astraweb.com>
In reply to#31560
Sorry for breaking threading, but the original post has not come through 
to me.

> On 18/10/2012 01:22, Rita wrote:
> Hi,
>
> Currently, I use a shell script to test how my system behaves before I
> deploy an application. For instance, I check if fileA, fileB, and fileC
> exist and if they do I go and start up my application.

Do you run the shell script once, before installing the application, or 
every time the application launches?

Do you realise that this is vulnerable to race conditions? E.g:

Time = 10am exactly: shell script runs, fileA etc exist;

Time = 10am and 1 millisecond: another process deletes fileA etc;

Time = 10am and 2 milliseconds: application launches, cannot find 
       fileA etc and crashes.


Depending on what your application does, this could be a security hole.

Regardless of what the shell script reports, to be robust your Python 
application needs to protect against the case that fileA etc are missing. 
Even if all it does is report an error, save the user's work and exit.


> This works great BUT
>
> I would like to use python and in particular unittest module to test my
> system and then deploy my app. I understand unittest is for functional
> testing but I think this too would be a case for it. Any thoughts? I am
> not looking for code in particular but just some ideas on how to use
> python better in situations like this.

Well, you *could* use unittest, but frankly I think that's a case of 
using a hammer to nail in screws. Unittest is awesome for what it does. 
It's not so well suited for this.

Compare these two pieces of code (untested, so they probably won't work 
exactly as given):

# sample 1
import os
import sys
for name in ['fileA', 'fileB', 'fileC']:
    if not os.path.exists(name):
        print('missing essential file %s' % name)
        sys.exit(1)

run_application()



# sample 2
import os
import sys
import unittest

class PreRunTest(unittest.TestCase):
    list_of_files = ['fileA', 'fileB', 'fileC']
    def testFilesExist(self):
        for name in self.list_of_files:
            assertTrue(os.path.exists(name)

total_tests, failed_tests = unittest.testmod()  # I think...

if failed_tests != 0:
    sys.exit(1)

run_application()



I think the first sample is much to be preferred, and not just because it 
is a couple of lines shorter. There's less magic involved.


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web