/[escript]/trunk/escriptcore/py_src/testing.py
ViewVC logotype

Contents of /trunk/escriptcore/py_src/testing.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4984 - (show annotations)
Mon Jun 2 02:50:34 2014 UTC (5 years, 2 months ago) by sshaw
File MIME type: text/x-python
File size: 3062 byte(s)
revamping testrunners, now uses automated discovery and allows running specific tests without modifying files (see escriptcore/py_src/testing.py for more info/examples)

1
2 ##############################################################################
3 #
4 # Copyright (c) 2014 by University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Open Software License version 3.0
9 # http://www.opensource.org/licenses/osl-3.0.php
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 """
18 A small set of functions to automate test discovery and running and also
19 allow specific tests to be run without modifying the test scripts themselves.
20
21 When imported into a test file, any external script can import the test file
22 module and run tests as they wish.
23
24 Both specific and general forms of the functions return the result state,
25 allowing access to list all skipped tests and other data kept by the
26 `TestTextResults` returned.
27
28 Examples:
29
30 Running all test classes
31 ```
32 import your_test_module_or_file as tests
33 tests.run_tests("your_test_module_or_file")
34 ```
35
36 Running specific test classes
37 ```
38 import your_test_module_or_file as tests
39 tests.run_tests("your_test_module_or_file", [tests.Test_classA,
40 tests.Test_classB])
41 ```
42
43 Running a specific test within a class
44 ```
45 import your_test_module_or_file as tests
46 tests.run_single_test(tests.Test_classname("test_functionname"))
47 ```
48
49 Printing the list of skipped tests
50 ```
51 results = tests.run_tests("name")
52 for skipped_test in results.skipped:
53 print(skipped_test)
54 ```
55
56 """
57
58 __copyright__="""Copyright (c) 2014 by University of Queensland
59 http://www.uq.edu.au
60 Primary Business: Queensland, Australia"""
61 __license__="""Licensed under the Open Software License version 3.0
62 http://www.opensource.org/licenses/osl-3.0.php"""
63 __url__="https://launchpad.net/escript-finley"
64
65 import esys.escriptcore.utestselect as unittest
66 import sys
67
68 def __add_tests(suite, modulename):
69 import inspect
70 clsmembers = inspect.getmembers(sys.modules[modulename], inspect.isclass)
71 for name, cls in clsmembers:
72 if modulename == cls.__module__ and name.startswith("Test_") \
73 and issubclass(cls, unittest.TestCase):
74 suite.addTest(unittest.makeSuite(cls))
75
76 def run_single_test(test, exit_on_failure=False):
77 suite = unittest.TestSuite()
78 suite.addTest(test)
79 s=unittest.TextTestRunner(verbosity=2).run(suite)
80 if exit_on_failure and not s.wasSuccessful():
81 sys.exit(1)
82 return s
83
84 def run_tests(modulename, classes = [], exit_on_failure = False):
85 suite = unittest.TestSuite()
86 if len(classes) == 0:
87 __add_tests(suite, modulename)
88 else:
89 for test_class in classes:
90 suite.addTest(unittest.makeSuite(test_class))
91 s=unittest.TextTestRunner(verbosity=2).run(suite)
92 if exit_on_failure and not s.wasSuccessful():
93 sys.exit(1)
94 return s
95

  ViewVC Help
Powered by ViewVC 1.1.26