Are there any python libraries to help test external python scripts
Question: I'd like to test some python scripts.

Are there any python libraries to help test external system behaviors(running
scripts, testing the contents of external files, managing input/output files,
and similar actions).

Also I tried making the scripts more api like to allow imports rather then
calling it directly for more unit test like tests. Changes include making
scripts easier to run interactively(factor lots of stuff into functions/module
values and make it less procedural, add parameter to silence stdout, passing
optional args to main) also serializing results in addition to the usual
output formats(even though the functions to generate the output files have a
medium amount of logic in them)).

Is this a good strategy or is it better to attempt to test scripts by running
them blackbox style and examining output.


Answer: **Test library**

I'll go ahead and suggest
[unittest](http://docs.python.org/library/unittest.html) _(even though it's
the top Google hit for "python unit testing" and you probably already know of
it)_. It's a very nice, easy to use, feature-ful library for unit testing.

**Test strategy**

Writing testable code is _hard_. Testing things like side-effects,
environments, and file output can take the **unit** right out of **unit
test**.

What I typically try to do is structure the code so that as little of it as
possible does I/O or other nasty things. Then all of that code can usually be
straightforwardly unit-tested.

For the parts that are hard to break into units, such as the command-line
interface, I test for file output etc.

**Conclusion**

  * use unit tests as much as possible
  * otherwise, use black-box tests 
  * constantly refactor code to make writing unit tests easier & more effective

