views:

76

answers:

2

In a bit of Python I'm writing (a command line and filter testing tool: claft) I wanted a simple way to invoke the built-in test suite (doctest) and I decided on the following:

if 'DOCTEST' in os.environ and os.environ['DOCTEST']==sys.argv[0]:
    _runDocTests()
    sys.exit()

Thus if the DOCTEST variable is set for some other program I'll just ignore it. In fact my test for this is just: DOCTEST=./claft ./claft or if I want to be verbose I can use: DOCTEST=./claft VERBOSE=1 ./claft So even if I leave DOCTEST=./claft in my environment the test code will only run when I invoke my program from within its own directory. If I switch to one of my test suites and invoke it using a relative PATH then I'm safe from inadvertently triggering this function.

Has anyone else used this sort of convention?

What are other suggestions or best practices for avoiding conflicts among environment variable names? For providing "hidden" access to test harness functionality?

(Also, if anyone wants to play with claft please feel free to give it a spin. It's pretty ugly code for now, and is barely a proof of concept. But it is minimally functional. It's also been a nice way to teach myself how to use Mercurial and bitbucket. The wiki and issue tracking are the best places to post feedback about claft).

+3  A: 

Since you're already doing command-line parsing, why not just add a --selftest option? You won't have to worry about any conflicts that way, and invocation will be easier.

derobert
A: 

Another hackish way to avoid namespace conflicts with the environment: looks for myprogname_DEBUG or the like.

Gregg Lind