From b0ccecf4e4d82101e991b9691fb455b8c45ade92 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 27 Feb 2012 18:12:42 +0000 Subject: [PATCH] Added machinery for running Python unit tests --- SConstruct | 5 + test/SConscript | 48 +++++++-- test/data/air-no-reactions.cti | 123 ++++++++++++++++++++++ test/data/air-no-reactions.xml | 181 +++++++++++++++++++++++++++++++++ test/python/runTests.py | 18 ++++ test/python/testThermo.py | 9 ++ 6 files changed, 378 insertions(+), 6 deletions(-) create mode 100644 test/data/air-no-reactions.cti create mode 100644 test/data/air-no-reactions.xml create mode 100644 test/python/runTests.py create mode 100644 test/python/testThermo.py diff --git a/SConstruct b/SConstruct index 92e883f77..552910a39 100644 --- a/SConstruct +++ b/SConstruct @@ -116,6 +116,11 @@ env = Environment(tools=toolchain+['textfile', 'subst', 'recursiveInstall', 'wix if os.name == 'nt' and 'TMP' in os.environ: env['ENV']['TMP'] = os.environ['TMP'] +# Fix an issue with Unicode sneaking into the environment on Windows +if os.name == 'nt': + for key,val in env['ENV'].iteritems(): + env['ENV'][key] = str(val) + add_RegressionTest(env) class defaults: pass diff --git a/test/SConscript b/test/SConscript index aaa7c903e..520c5c88b 100644 --- a/test/SConscript +++ b/test/SConscript @@ -8,21 +8,57 @@ localenv.Append(CPPPATH=['#ext/gtest/include', '#include'], LIBPATH='#build/lib', LIBS=['gtest'] + localenv['cantera_libs']) -def gtestRunner(target, source, env): +localenv['ENV']['PYTHONPATH'] = Dir('#interfaces/python').abspath +localenv['ENV']['CANTERA_DATA'] = Dir('#data/inputs').abspath + +def testRunner(target, source, env): + """SCons Action to run a compiled test program""" program = source[0] - code = subprocess.call([program.abspath]) + code = subprocess.call([program.abspath], env=env['ENV']) if not code: open(target[0].path, 'w').write(time.asctime()+'\n') - return 0 + return code -def addTest(subdir, progName): + +def scriptRunner(target, source, env): + """Scons Action to run a test script using the specified interpreter""" + interpreter = env['test_interpreter'] + code = subprocess.call([interpreter, str(source[0])], + env=env['ENV']) + if not code: + open(target[0].path, 'w').write(time.asctime()+'\n') + return code + + +def addTestProgram(subdir, progName): + """ + Compile a test program and create a targets for running + and resetting the test. + """ program = localenv.Program(pjoin(subdir, progName), pjoin(subdir, '%s.cpp' % progName)) passedFile = File(pjoin(str(program[0].dir), '%s.passed' % program[0].name)) - run_program = localenv.Command(passedFile, program, gtestRunner) + run_program = localenv.Command(passedFile, program, testRunner) Alias('newtest', run_program) if os.path.exists(passedFile.abspath): Alias('newtest-reset', localenv.Command('reset-%s%s' % (subdir, progName), [], [Delete(passedFile.abspath)])) -addTest('thermo', 'nasapoly') + +def addTestScript(subdir, script, interpreter): + """ + Create targets for running and resetting a test script. + """ + testenv = localenv.Clone() + testenv['test_interpreter'] = interpreter + passedFile = File(pjoin(subdir, '%s.passed' % (script))) + run_program = testenv.Command(passedFile, pjoin(subdir, script), scriptRunner) + Alias('newtest', run_program) + if os.path.exists(passedFile.abspath): + Alias('newtest-reset', testenv.Command('reset-%s%s' % (subdir, script), + [], [Delete(passedFile.abspath)])) + + +# Instantiate tests +addTestProgram('thermo', 'nasapoly') +addTestScript('python', 'runTests.py', interpreter=sys.executable) diff --git a/test/data/air-no-reactions.cti b/test/data/air-no-reactions.cti new file mode 100644 index 000000000..16cceef42 --- /dev/null +++ b/test/data/air-no-reactions.cti @@ -0,0 +1,123 @@ +units(length = "cm", time = "s", quantity = "mol", act_energy = "cal/mol") + +ideal_gas(name = "air", + elements = " O N Ar ", + species = """ O O2 N NO NO2 N2O N2 AR """, + initial_state = state(temperature = 300.0, + pressure = OneAtm, + mole_fractions = 'O2:0.21, N2:0.78, AR:0.01') ) + +ideal_gas(name = "notair", + elements = " O N ", + species = """ O O2 N NO NO2 N2O N2 """, + initial_state = state(temperature = 900.0, + pressure = 5*OneAtm, + mass_fractions = 'O2:0.5, N2:0.5') ) + +#------------------------------------------------------------------------------- +# Species data +#------------------------------------------------------------------------------- + +species(name = "O", + atoms = " O:1 ", + thermo = ( + NASA( [ 200.00, 1000.00], [ 3.168267100E+00, -3.279318840E-03, + 6.643063960E-06, -6.128066240E-09, 2.112659710E-12, + 2.912225920E+04, 2.051933460E+00] ), + NASA( [ 1000.00, 3500.00], [ 2.569420780E+00, -8.597411370E-05, + 4.194845890E-08, -1.001777990E-11, 1.228336910E-15, + 2.921757910E+04, 4.784338640E+00] ) + ), + note = "L 1/90" + ) + +species(name = "O2", + atoms = " O:2 ", + thermo = ( + NASA( [ 200.00, 1000.00], [ 3.782456360E+00, -2.996734160E-03, + 9.847302010E-06, -9.681295090E-09, 3.243728370E-12, + -1.063943560E+03, 3.657675730E+00] ), + NASA( [ 1000.00, 3500.00], [ 3.282537840E+00, 1.483087540E-03, + -7.579666690E-07, 2.094705550E-10, -2.167177940E-14, + -1.088457720E+03, 5.453231290E+00] ) + ), + note = "TPIS89" + ) + +species(name = "N", + atoms = " N:1 ", + thermo = ( + NASA( [ 200.00, 1000.00], [ 2.500000000E+00, 0.000000000E+00, + 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + 5.610463700E+04, 4.193908700E+00] ), + NASA( [ 1000.00, 6000.00], [ 2.415942900E+00, 1.748906500E-04, + -1.190236900E-07, 3.022624500E-11, -2.036098200E-15, + 5.613377300E+04, 4.649609600E+00] ) + ), + note = "L 6/88" + ) + +species(name = "NO", + atoms = " N:1 O:1 ", + thermo = ( + NASA( [ 200.00, 1000.00], [ 4.218476300E+00, -4.638976000E-03, + 1.104102200E-05, -9.336135400E-09, 2.803577000E-12, + 9.844623000E+03, 2.280846400E+00] ), + NASA( [ 1000.00, 6000.00], [ 3.260605600E+00, 1.191104300E-03, + -4.291704800E-07, 6.945766900E-11, -4.033609900E-15, + 9.920974600E+03, 6.369302700E+00] ) + ), + note = "RUS 78" + ) + +species(name = "NO2", + atoms = " N:1 O:2 ", + thermo = ( + NASA( [ 200.00, 1000.00], [ 3.944031200E+00, -1.585429000E-03, + 1.665781200E-05, -2.047542600E-08, 7.835056400E-12, + 2.896617900E+03, 6.311991700E+00] ), + NASA( [ 1000.00, 6000.00], [ 4.884754200E+00, 2.172395600E-03, + -8.280690600E-07, 1.574751000E-10, -1.051089500E-14, + 2.316498300E+03, -1.174169500E-01] ) + ), + note = "L 7/88" + ) + +species(name = "N2O", + atoms = " N:2 O:1 ", + thermo = ( + NASA( [ 200.00, 1000.00], [ 2.257150200E+00, 1.130472800E-02, + -1.367131900E-05, 9.681980600E-09, -2.930718200E-12, + 8.741774400E+03, 1.075799200E+01] ), + NASA( [ 1000.00, 6000.00], [ 4.823072900E+00, 2.627025100E-03, + -9.585087400E-07, 1.600071200E-10, -9.775230300E-15, + 8.073404800E+03, -2.201720700E+00] ) + ), + note = "L 7/88" + ) + +species(name = "N2", + atoms = " N:2 ", + thermo = ( + NASA( [ 300.00, 1000.00], [ 3.298677000E+00, 1.408240400E-03, + -3.963222000E-06, 5.641515000E-09, -2.444854000E-12, + -1.020899900E+03, 3.950372000E+00] ), + NASA( [ 1000.00, 5000.00], [ 2.926640000E+00, 1.487976800E-03, + -5.684760000E-07, 1.009703800E-10, -6.753351000E-15, + -9.227977000E+02, 5.980528000E+00] ) + ), + note = "121286" + ) + +species(name = "AR", + atoms = " Ar:1 ", + thermo = ( + NASA( [ 300.00, 1000.00], [ 2.500000000E+00, 0.000000000E+00, + 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + -7.453750000E+02, 4.366000000E+00] ), + NASA( [ 1000.00, 5000.00], [ 2.500000000E+00, 0.000000000E+00, + 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + -7.453750000E+02, 4.366000000E+00] ) + ), + note = "120186" + ) diff --git a/test/data/air-no-reactions.xml b/test/data/air-no-reactions.xml new file mode 100644 index 000000000..2a0317465 --- /dev/null +++ b/test/data/air-no-reactions.xml @@ -0,0 +1,181 @@ + + + + + + + O N Ar + O O2 N NO NO2 N2O N2 AR + + 300.0 + 101325.0 + O2:0.21, N2:0.78, AR:0.01 + + + + + + + + + O N + O O2 N NO NO2 N2O N2 + + 900.0 + 506625.0 + O2:0.5, N2:0.5 + + + + + + + + + + + + O:1 + L 1/90 + + + + 3.168267100E+00, -3.279318840E-03, 6.643063960E-06, -6.128066240E-09, + 2.112659710E-12, 2.912225920E+04, 2.051933460E+00 + + + + 2.569420780E+00, -8.597411370E-05, 4.194845890E-08, -1.001777990E-11, + 1.228336910E-15, 2.921757910E+04, 4.784338640E+00 + + + + + + + O:2 + TPIS89 + + + + 3.782456360E+00, -2.996734160E-03, 9.847302010E-06, -9.681295090E-09, + 3.243728370E-12, -1.063943560E+03, 3.657675730E+00 + + + + 3.282537840E+00, 1.483087540E-03, -7.579666690E-07, 2.094705550E-10, + -2.167177940E-14, -1.088457720E+03, 5.453231290E+00 + + + + + + + N:1 + L 6/88 + + + + 2.500000000E+00, 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + 0.000000000E+00, 5.610463700E+04, 4.193908700E+00 + + + + 2.415942900E+00, 1.748906500E-04, -1.190236900E-07, 3.022624500E-11, + -2.036098200E-15, 5.613377300E+04, 4.649609600E+00 + + + + + + + O:1 N:1 + RUS 78 + + + + 4.218476300E+00, -4.638976000E-03, 1.104102200E-05, -9.336135400E-09, + 2.803577000E-12, 9.844623000E+03, 2.280846400E+00 + + + + 3.260605600E+00, 1.191104300E-03, -4.291704800E-07, 6.945766900E-11, + -4.033609900E-15, 9.920974600E+03, 6.369302700E+00 + + + + + + + O:2 N:1 + L 7/88 + + + + 3.944031200E+00, -1.585429000E-03, 1.665781200E-05, -2.047542600E-08, + 7.835056400E-12, 2.896617900E+03, 6.311991700E+00 + + + + 4.884754200E+00, 2.172395600E-03, -8.280690600E-07, 1.574751000E-10, + -1.051089500E-14, 2.316498300E+03, -1.174169500E-01 + + + + + + + O:1 N:2 + L 7/88 + + + + 2.257150200E+00, 1.130472800E-02, -1.367131900E-05, 9.681980600E-09, + -2.930718200E-12, 8.741774400E+03, 1.075799200E+01 + + + + 4.823072900E+00, 2.627025100E-03, -9.585087400E-07, 1.600071200E-10, + -9.775230300E-15, 8.073404800E+03, -2.201720700E+00 + + + + + + + N:2 + 121286 + + + + 3.298677000E+00, 1.408240400E-03, -3.963222000E-06, 5.641515000E-09, + -2.444854000E-12, -1.020899900E+03, 3.950372000E+00 + + + + 2.926640000E+00, 1.487976800E-03, -5.684760000E-07, 1.009703800E-10, + -6.753351000E-15, -9.227977000E+02, 5.980528000E+00 + + + + + + + Ar:1 + 120186 + + + + 2.500000000E+00, 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + 0.000000000E+00, -7.453750000E+02, 4.366000000E+00 + + + + 2.500000000E+00, 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + 0.000000000E+00, -7.453750000E+02, 4.366000000E+00 + + + + + + diff --git a/test/python/runTests.py b/test/python/runTests.py new file mode 100644 index 000000000..b0965eff8 --- /dev/null +++ b/test/python/runTests.py @@ -0,0 +1,18 @@ +""" +Unit tests for the Cantera Python module. + +This script gathers all the tests defined in all of the test.py +files, runs them, and prints a report. +""" +import unittest + +import Cantera + +if __name__ == '__main__': + print '\n* INFO: using Cantera module found at this location:' + print '* ', repr(Cantera.__file__), '\n' + + loader = unittest.TestLoader() + runner = unittest.TextTestRunner(verbosity=2) + suite = loader.loadTestsFromName('testThermo') + runner.run(suite) diff --git a/test/python/testThermo.py b/test/python/testThermo.py new file mode 100644 index 000000000..6bfa06891 --- /dev/null +++ b/test/python/testThermo.py @@ -0,0 +1,9 @@ +import unittest + +import Cantera as ct + +class ImportTest(unittest.TestCase): + def testCtiImport(self): + gas = ct.importPhase('test/data/air-no-reactions.cti', 'air') + + self.assertAlmostEqual(gas.temperature(), 300)