Skip to content

Commit 1303fe9

Browse files
committed
Splitting test class into unit and integration tests
1 parent 1576631 commit 1303fe9

File tree

5 files changed

+110
-108
lines changed

5 files changed

+110
-108
lines changed

fortran_tests/testsuites.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
suite: builtin
33
options:
44

5-
[UnitTests]
5+
[Examples]
66
obtain: shutil.copytree('../../examples/in', 'examples', dirs_exist_ok=True)
77
path: examples
88
suite: builtin

fprettify/tests/fortrantests.py

+97-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
###############################################################################
2020

2121

22+
import sys
2223
import hashlib
2324
import logging
2425
import io
@@ -28,12 +29,97 @@
2829
import configparser
2930
import shutil
3031
import shlex
32+
from datetime import datetime
3133
import fprettify
32-
from fprettify.tests.test_common import TEST_MAIN_DIR, TEST_EXT_DIR, BACKUP_DIR, RESULT_DIR, RESULT_FILE, FAILED_FILE, FprettifyTestCase, joinpath
34+
from fprettify.tests.test_common import _MYPATH, FprettifyTestCase, joinpath
35+
36+
_TIMESTAMP = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
37+
38+
# main directory for running tests
39+
TEST_MAIN_DIR = joinpath(_MYPATH, r'../../fortran_tests')
40+
41+
# directory for external Fortran code
42+
TEST_EXT_DIR = joinpath(TEST_MAIN_DIR, r'test_code')
43+
44+
# directory containing Fortran examples
45+
EXAMPLE_DIR = joinpath(_MYPATH, r'../../examples/in')
46+
47+
# backup directory
48+
BACKUP_DIR = joinpath(TEST_MAIN_DIR, r'test_code_in_' + _TIMESTAMP)
49+
50+
# where to store summarized results
51+
RESULT_DIR = joinpath(TEST_MAIN_DIR, r'test_results')
52+
53+
# expected hash-sums
54+
RESULT_FILE = joinpath(RESULT_DIR, r'expected_results')
55+
56+
# test failures
57+
FAILED_FILE = joinpath(RESULT_DIR, r'failed_results')
3358

3459

3560
fprettify.set_fprettify_logger(logging.ERROR)
3661

62+
class FprettifyIntegrationTestCase(FprettifyTestCase):
63+
def shortDescription(self):
64+
"""don't print doc string of testmethod"""
65+
return None
66+
67+
def setUp(self):
68+
"""
69+
setUp to be recognized by unittest.
70+
We have large files to compare, raise the limit
71+
"""
72+
self.maxDiff = None
73+
74+
@classmethod
75+
def setUpClass(cls):
76+
"""
77+
setUpClass to be recognized by unittest.
78+
"""
79+
80+
cls.n_success = 0
81+
cls.n_parsefail = 0
82+
cls.n_internalfail = 0
83+
cls.n_unexpectedfail = 0
84+
85+
FprettifyIntegrationTestCase.eprint("-" * 70)
86+
FprettifyIntegrationTestCase.eprint("recognized Fortran files")
87+
FprettifyIntegrationTestCase.eprint(", ".join(fprettify.FORTRAN_EXTENSIONS))
88+
FprettifyIntegrationTestCase.eprint("-" * 70)
89+
FprettifyIntegrationTestCase.eprint("Applying fprettify to Fortran files in " + TEST_EXT_DIR)
90+
FprettifyIntegrationTestCase.eprint("Writing backup of original files to " + BACKUP_DIR)
91+
FprettifyIntegrationTestCase.eprint("Storing expected results in " + RESULT_FILE)
92+
FprettifyIntegrationTestCase.eprint("Storing failed results in " + FAILED_FILE)
93+
FprettifyIntegrationTestCase.eprint("-" * 70)
94+
95+
@classmethod
96+
def tearDownClass(cls):
97+
"""
98+
tearDownClass to be recognized by unittest. Used for test summary
99+
output.
100+
"""
101+
if cls.n_parsefail + cls.n_internalfail > 0:
102+
format = "{:<20}{:<6}"
103+
FprettifyIntegrationTestCase.eprint('\n' + "=" * 70)
104+
FprettifyIntegrationTestCase.eprint("IGNORED errors: invalid or old Fortran")
105+
FprettifyIntegrationTestCase.eprint("-" * 70)
106+
FprettifyIntegrationTestCase.eprint(format.format("parse errors: ", cls.n_parsefail))
107+
FprettifyIntegrationTestCase.eprint(format.format("internal errors: ", cls.n_internalfail))
108+
109+
@staticmethod
110+
def write_result(filename, content, sep_str): # pragma: no cover
111+
with io.open(filename, 'a', encoding='utf-8') as outfile:
112+
outfile.write(sep_str.join(content) + '\n')
113+
114+
@staticmethod
115+
def eprint(*args, **kwargs):
116+
"""
117+
Print to stderr - to print output compatible with default unittest output.
118+
"""
119+
120+
print(*args, file=sys.stderr, flush=True, **kwargs)
121+
122+
37123
def generate_suite(suite=None, name=None):
38124
import git
39125
config = configparser.ConfigParser()
@@ -56,15 +142,15 @@ def generate_suite(suite=None, name=None):
56142
os.chdir(orig)
57143

58144
addtestcode(code['path'], code['options'])
59-
return FprettifyTestCase
145+
return FprettifyIntegrationTestCase
60146

61147
def addtestcode(code_path, options):
62148
print(f"creating test cases from {code_path} ...")
63149
# dynamically create test cases from fortran files in test directory
64150
for dirpath, _, filenames in os.walk(joinpath(TEST_EXT_DIR, code_path)):
65151
for example in [f for f in filenames if any(f.endswith(_) for _ in fprettify.FORTRAN_EXTENSIONS)]:
66152
rel_dirpath = os.path.relpath(dirpath, start=TEST_EXT_DIR)
67-
addtestmethod(FprettifyTestCase, rel_dirpath, example, options)
153+
addtestmethod(FprettifyIntegrationTestCase, rel_dirpath, example, options)
68154

69155
def addtestmethod(testcase, fpath, ffile, options):
70156
"""add a test method for each example."""
@@ -107,19 +193,19 @@ def test_result(path, info):
107193
test_info = "checksum"
108194
test_content = test_result(example, m.hexdigest())
109195

110-
FprettifyTestCase.n_success += 1
196+
FprettifyIntegrationTestCase.n_success += 1
111197
except fprettify.FprettifyParseException as e:
112198
test_info = "parse error"
113199
fprettify.log_exception(e, test_info, level="warning")
114200
test_content = test_result(example, test_info)
115-
FprettifyTestCase.n_parsefail += 1
201+
FprettifyIntegrationTestCase.n_parsefail += 1
116202
except fprettify.FprettifyInternalException as e:
117203
test_info = "internal error"
118204
fprettify.log_exception(e, test_info, level="warning")
119205
test_content = test_result(example, test_info)
120-
FprettifyTestCase.n_internalfail += 1
206+
FprettifyIntegrationTestCase.n_internalfail += 1
121207
except: # pragma: no cover
122-
FprettifyTestCase.n_unexpectedfail += 1
208+
FprettifyIntegrationTestCase.n_unexpectedfail += 1
123209
raise
124210

125211
# overwrite example
@@ -141,7 +227,7 @@ def test_result(path, info):
141227
line_content = line.strip().split(sep_str)
142228
if line_content[0] == test_content[0]:
143229
found = True
144-
FprettifyTestCase.eprint(test_info, end=" ")
230+
FprettifyIntegrationTestCase.eprint(test_info, end=" ")
145231
msg = '{} (old) != {} (new)'.format(
146232
line_content[1], test_content[1])
147233
if test_info == "checksum" and outstring.count('\n') < 10000:
@@ -153,14 +239,14 @@ def test_result(path, info):
153239
testcase.assertEqual(
154240
line_content[1], test_content[1], msg)
155241
except AssertionError: # pragma: no cover
156-
FprettifyTestCase.write_result(
242+
FprettifyIntegrationTestCase.write_result(
157243
FAILED_FILE, test_content, sep_str)
158244
raise
159245
break
160246

161247
if not found: # pragma: no cover
162-
FprettifyTestCase.eprint(test_info + " new", end=" ")
163-
FprettifyTestCase.write_result(RESULT_FILE, test_content, sep_str)
248+
FprettifyIntegrationTestCase.eprint(test_info + " new", end=" ")
249+
FprettifyIntegrationTestCase.write_result(RESULT_FILE, test_content, sep_str)
164250

165251
# not sure why this even works, using "test something" (with a space) as function name...
166252
# however it gives optimal test output

fprettify/tests/test_common.py

+1-86
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
###############################################################################
2020
import os, sys, io
2121
import inspect
22-
import hashlib
23-
from datetime import datetime
2422
import unittest
2523
import fprettify
2624

@@ -31,94 +29,11 @@ def joinpath(path1, path2):
3129
_MYPATH = os.path.dirname(os.path.abspath(
3230
inspect.getfile(inspect.currentframe())))
3331

34-
_TIMESTAMP = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
35-
36-
# main directory for running tests
37-
TEST_MAIN_DIR = joinpath(_MYPATH, r'../../fortran_tests')
38-
39-
# directory for external Fortran code
40-
TEST_EXT_DIR = joinpath(TEST_MAIN_DIR, r'test_code')
41-
42-
# directory containing Fortran examples
43-
EXAMPLE_DIR = joinpath(_MYPATH, r'../../examples/in')
44-
45-
# backup directory
46-
BACKUP_DIR = joinpath(TEST_MAIN_DIR, r'test_code_in_' + _TIMESTAMP)
47-
48-
# where to store summarized results
49-
RESULT_DIR = joinpath(TEST_MAIN_DIR, r'test_results')
50-
51-
# expected hash-sums
52-
RESULT_FILE = joinpath(RESULT_DIR, r'expected_results')
53-
54-
# test failures
55-
FAILED_FILE = joinpath(RESULT_DIR, r'failed_results')
56-
5732
# path to fprettify
5833
RUNSCRIPT = joinpath(_MYPATH, r"../../fprettify.py")
5934

6035

6136
class FprettifyTestCase(unittest.TestCase):
6237
"""
63-
test class to be recognized by unittest.
38+
test class to be recognized by unittest, specialized for fprettify tests.
6439
"""
65-
66-
def shortDescription(self):
67-
"""don't print doc string of testmethod"""
68-
return None
69-
70-
def setUp(self):
71-
"""
72-
setUp to be recognized by unittest.
73-
We have large files to compare, raise the limit
74-
"""
75-
self.maxDiff = None
76-
77-
@classmethod
78-
def setUpClass(cls):
79-
"""
80-
setUpClass to be recognized by unittest.
81-
"""
82-
83-
cls.n_success = 0
84-
cls.n_parsefail = 0
85-
cls.n_internalfail = 0
86-
cls.n_unexpectedfail = 0
87-
88-
FprettifyTestCase.eprint("-" * 70)
89-
FprettifyTestCase.eprint("recognized Fortran files")
90-
FprettifyTestCase.eprint(", ".join(fprettify.FORTRAN_EXTENSIONS))
91-
FprettifyTestCase.eprint("-" * 70)
92-
FprettifyTestCase.eprint("Applying fprettify to Fortran files in " + TEST_EXT_DIR)
93-
FprettifyTestCase.eprint("Writing backup of original files to " + BACKUP_DIR)
94-
FprettifyTestCase.eprint("Storing expected results in " + RESULT_FILE)
95-
FprettifyTestCase.eprint("Storing failed results in " + FAILED_FILE)
96-
FprettifyTestCase.eprint("-" * 70)
97-
98-
@classmethod
99-
def tearDownClass(cls):
100-
"""
101-
tearDownClass to be recognized by unittest. Used for test summary
102-
output.
103-
"""
104-
if cls.n_parsefail + cls.n_internalfail > 0:
105-
format = "{:<20}{:<6}"
106-
FprettifyTestCase.eprint('\n' + "=" * 70)
107-
FprettifyTestCase.eprint("IGNORED errors: invalid or old Fortran")
108-
FprettifyTestCase.eprint("-" * 70)
109-
FprettifyTestCase.eprint(format.format("parse errors: ", cls.n_parsefail))
110-
FprettifyTestCase.eprint(format.format("internal errors: ", cls.n_internalfail))
111-
112-
@staticmethod
113-
def write_result(filename, content, sep_str): # pragma: no cover
114-
with io.open(filename, 'a', encoding='utf-8') as outfile:
115-
outfile.write(sep_str.join(content) + '\n')
116-
117-
@staticmethod
118-
def eprint(*args, **kwargs):
119-
"""
120-
Print to stderr - to print output compatible with default unittest output.
121-
"""
122-
123-
print(*args, file=sys.stderr, flush=True, **kwargs)
124-

fprettify/tests/unittests.py

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import logging
2727
import io
2828
import subprocess
29-
import shlex
3029

3130
sys.stderr = io.TextIOWrapper(
3231
sys.stderr.detach(), encoding='UTF-8', line_buffering=True)

run_tests.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
import unittest
2222
from fprettify.tests.unittests import FprettifyUnitTestCase
23-
from fprettify.tests.fortrantests import generate_suite
24-
from fprettify.tests.test_common import FAILED_FILE, RESULT_FILE, FprettifyTestCase
23+
from fprettify.tests.fortrantests import generate_suite, FAILED_FILE, RESULT_FILE
2524
import fileinput
2625
import io
2726
import os
@@ -44,18 +43,21 @@
4443
if args.suite[:2] == suite_default and len(args.suite) > 2:
4544
args.suite = args.suite[2:]
4645

46+
test_cases = []
47+
4748
if args.name:
48-
testCase = generate_suite(name=args.name)
49+
test_cases.append(generate_suite(name=args.name))
4950
else:
50-
test_suite = unittest.TestSuite()
5151
for suite in args.suite:
5252
if suite == "unittests":
53-
test_loaded = unittest.TestLoader().loadTestsFromTestCase(FprettifyUnitTestCase)
54-
test_suite.addTest(test_loaded)
53+
test_cases.append(FprettifyUnitTestCase)
5554
else:
56-
testCase = generate_suite(suite=suite)
57-
test_loaded = unittest.TestLoader().loadTestsFromTestCase(testCase)
58-
test_suite.addTest(test_loaded)
55+
test_cases.append(generate_suite(suite=suite))
56+
57+
test_suite = unittest.TestSuite()
58+
for test_case in test_cases:
59+
test_loaded = unittest.TestLoader().loadTestsFromTestCase(test_case)
60+
test_suite.addTest(test_loaded)
5961

6062
unittest.TextTestRunner(verbosity=2).run(test_suite)
6163

0 commit comments

Comments
 (0)