From 61bb87524818f1c24b48b9a737ba044fefce2a91 Mon Sep 17 00:00:00 2001 From: Mike Beachy Date: Sat, 22 Feb 2014 17:33:18 -0500 Subject: [PATCH] add test framework - add the ability to run tests from the top level directory via 'python setup.py test' - translate runtest.py into a test that runs under this framework - add a __main__ section to Tests/runtest.py and change its indent to 4 spaces; make no other changes to this script --- Tests/__init__.py | 0 Tests/pkacalc_test.py | 68 +++++++++++++++++++++++++++++++++++++++++++ Tests/runtest.py | 64 +++++++++++++++++++--------------------- setup.py | 1 + 4 files changed, 99 insertions(+), 34 deletions(-) create mode 100644 Tests/__init__.py create mode 100644 Tests/pkacalc_test.py diff --git a/Tests/__init__.py b/Tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Tests/pkacalc_test.py b/Tests/pkacalc_test.py new file mode 100644 index 0000000..04f896e --- /dev/null +++ b/Tests/pkacalc_test.py @@ -0,0 +1,68 @@ + +import os +import re +from subprocess import call +import sys +import unittest + +# Setting this up as a direct translation of the original runtest.py script +# that will be run as part of 'python setup.py test'. This takes on the +# order of 30s to run. + +class SystemTest(unittest.TestCase): + """ + Run the program and compare against reference results. + """ + def test_pka_calc(self): + pdbs = ['1FTJ-Chain-A', + '1HPX', + '4DFR'] + + test_dir = os.path.dirname(__file__) + base_dir = os.path.dirname(test_dir) + + executable = os.path.join(base_dir, "scripts", "propka31.py") + + env = { "PYTHONPATH" : base_dir } + + for pdb in pdbs: + input_filename = os.path.join(test_dir, "pdb", pdb + ".pdb") + output_filename = os.path.join(test_dir, pdb + ".out") + + output_file = open(output_filename, "w") + call([sys.executable, executable, input_filename], + stdout=output_file, env=env) + output_file.close() + + # Check pka predictions. + ref = open(os.path.join(test_dir, "results", pdb + ".dat")) + output = open(output_filename) + + atpka = False + errors = [] + for line in output: + if not atpka: + # Start testing pka values. + if "model-pKa" in line: + atpka = True + continue + + m = re.search('([0-9]+\.[0-9]+)', line) + if not m: + break + + expected_value = float(ref.readline()) + value = float(m.group(0)) + + if(value != expected_value): + identity = line[:m.start()].strip() + errors.append("%12s %8.2f %8.2f" % + (identity, expected_value, value)) + + ref.close() + output.close() + + if errors: + error_header = " Group Expected Calculated\n" + self.Fail("Unexpected pKa values:\n" + error_header + + "\n".join(errors)) diff --git a/Tests/runtest.py b/Tests/runtest.py index e73517a..3ea030c 100755 --- a/Tests/runtest.py +++ b/Tests/runtest.py @@ -9,43 +9,39 @@ from subprocess import call import os, re import sys -pdbs = ['1FTJ-Chain-A', - '1HPX', - '4DFR'] - -for pdb in pdbs: - print('') - print('RUNNING '+pdb) - - # Run pka calculation - call([sys.executable, '../scripts/propka31.py','pdb/'+pdb+'.pdb'], stdout = open(pdb+'.out', 'w+')) - - # Test pka predictiona - result = open('results/'+pdb+'.dat','r') - atpka = False - for line in open(pdb+'.pka', 'r').readlines(): - if not atpka: - if "model-pKa" in line: - # test pka - atpka = True - continue - else: - continue - if "-" in line: - # done testing - atpka = False - continue - - r = float(result.readline()) - m = re.search('([0-9]+\.[0-9]+)', line) - - if(float(m.group(0)) != r): - print(" ERR:") - print(line) - print(" "+"should be: "+str(r)) +if __name__ == "__main__": + pdbs = ['1FTJ-Chain-A', + '1HPX', + '4DFR'] + for pdb in pdbs: + print('') + print('RUNNING '+pdb) + # Run pka calculation + call([sys.executable, '../scripts/propka31.py','pdb/'+pdb+'.pdb'], stdout = open(pdb+'.out', 'w+')) + # Test pka predictiona + result = open('results/'+pdb+'.dat','r') + atpka = False + for line in open(pdb+'.pka', 'r').readlines(): + if not atpka: + if "model-pKa" in line: + # test pka + atpka = True + continue + else: + continue + if "-" in line: + # done testing + atpka = False + continue + r = float(result.readline()) + m = re.search('([0-9]+\.[0-9]+)', line) + if(float(m.group(0)) != r): + print(" ERR:") + print(line) + print(" "+"should be: "+str(r)) diff --git a/setup.py b/setup.py index 1bee359..94e7ea4 100644 --- a/setup.py +++ b/setup.py @@ -48,4 +48,5 @@ using the tutorial http://propka.ki.ku.dk/~luca/wiki/index.php/PROPKA_3.1_Tutori ], }, zip_safe=True, + test_suite="Tests", )