how to run a set of Python unit tests
Question: I am running a set of unit tests using a Bash script. What is a more Pythonic
way of doing this generally?

Assuming I cannot change the unit tests, what would be the most Pythonic way
of doing this?

The Bash script to run all of the tests is as follows:

    
    
    #!/bin/bash
    
    function PJTUnitTests () {
        exitCode=0
        for test in $(ls PACKAGE_DIRECTORY/test/test_transform.py PACKAGE_DIRECTORY/test/test_trf*.py); do
            name=$(basename $test)
            echo "running ${name}"
            ${test} &> ${name}.test
            if [ $? != "0" ]; then 
               echo "$(date) "${test}" failed" | tee -a test.fail
               exitCode=1
            else
            echo "$(date) ${test} ok" | tee -a test.ok
            fi
        done
        if [ $exitCode != "0" ]; then
            echo "At least one test failed -- see summary file test.fail for details."
        else
            echo "All tests passed."
        fi
    }
    
    PJTUnitTests
    

A representative unit test is the following:

    
    
    import json
    import subprocess
    import os
    import os.path
    import sys
    import unittest
    
    from PACKAGE.MODULE1 import msg
    
    class Echotest(unittest.TestCase):
    
        def test_runEcho(self):
            cmd = ['Echo_1.py']
            cmd.extend(['--testInt', '1234'])
            cmd.extend(['--testFloat', '-1.212'])
            cmd.extend(['--testIntList', '1,2,3,4,5,6'])
            cmd.extend(['--testSubstepList', 'all:juice', 'jane:apple', 'bob:orange', 'alice:pear'])
            cmd.extend(['--testSubstepInt', 'all:34', 'jane:1', 'bob:2', 'alice:-3'])
            cmd.extend(['--testSubstepBool', 'all:True', 'jane:false', 'bob:tRuE', 'alice:FaLse'])
            msg.info('Will run this transform: {0}'.format(cmd))
            p = subprocess.Popen(cmd, shell = False, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, bufsize = 1)
            while p.poll() is None:
                line = p.stdout.readline()
                sys.stdout.write(line)
            # Hoover up remaining buffered output lines.
            for line in p.stdout:
                sys.stdout.write(line)
            self.assertEqual(p.returncode, 0)
    
            # Now load metadata and test a few important values.
            with open('jobReport.json') as jr:
                md = json.load(jr)
                self.assertEqual(isinstance(md, dict), True)
    
    if __name__ == '__main__':
        unittest.main()
    


Answer: I thing the most pythonic would be to use the testing frameworks - nose,
py.test are the most popular(IMHO). So in your bash script pass control
variables to the framework used and you will be more pythonic, I guess.

