Changeset 50
- Timestamp:
- 04/09/09 13:19:19 (3 years ago)
- Location:
- trunk/mgunit
- Files:
-
- 1 added
- 1 removed
- 5 modified
-
docs/findgen_ut__define.pro (modified) (4 diffs)
-
docs/indgen_uts__define.pro (modified) (2 diffs)
-
docs/indgen_ut__define.pro (modified) (4 diffs)
-
docs/using-mgunit.txt (modified) (2 diffs)
-
Makefile (modified) (2 diffs)
-
src/mgunit-templates.xml (added)
-
src/test-templates.xml (deleted)
Legend:
- Unmodified
- Added
- Removed
-
trunk/mgunit/docs/findgen_ut__define.pro
r49 r50 10 10 11 11 ;+ 12 ; This test should pass the assertion and return 1 (i.e. success). Tests can 13 ; also return 0 or generate an error to indicate failure. 14 ;- 15 function findgen_ut::test_basic 16 compile_opt strictarr 17 18 a = findgen(5) 19 assert, array_equal(a, [0.0, 1.0, 2.0, 3.0, 4.0]), 'Correct elements' 20 21 return, 1 22 end 23 24 25 ;+ 12 26 ; This test fails because the assertion is wrong. 13 27 ;- 14 function findgen_ut::test 128 function findgen_ut::test_fail_example 15 29 compile_opt strictarr 16 30 … … 23 37 24 38 ;+ 25 ; This test should pass the assertion and return 1 (i.e. success). Tests can26 ; also return 0 or generate an error to indicate failure.27 ;-28 function findgen_ut::test229 compile_opt strictarr30 31 a = findgen(5)32 assert, array_equal(a, [0.0, 1.0, 2.0, 3.0, 4.0]), 'Correct elements'33 34 return, 135 end36 37 38 ;+39 39 ; This is a test that will pass because the code of the test is supposed to 40 40 ; cause an error. To do this kind of test, use the "error_is_pass" batch file. 41 41 ;- 42 function findgen_ut::test 342 function findgen_ut::test_error 43 43 compile_opt strictarr 44 45 44 @error_is_pass 46 45 … … 55 54 ; "error_is_fail" batch file. IO errors don't normally cause a test to fail. 56 55 ;- 57 function findgen_ut::test4 58 compile_opt strictarr 59 56 function findgen_ut::test_baderror 57 compile_opt strictarr 60 58 @error_is_fail 61 59 … … 72 70 compile_opt strictarr 73 71 74 define = { findgen_ut, inherits MG testCase }72 define = { findgen_ut, inherits MGutTestCase } 75 73 end -
trunk/mgunit/docs/indgen_uts__define.pro
r49 r50 4 4 compile_opt strictarr 5 5 6 if (~self->mg testsuite::init(_extra=e)) then return, 06 if (~self->mguttestsuite::init(_extra=e)) then return, 0 7 7 8 8 ;self->add, ['indgen_ut', 'findgen_ut'] … … 16 16 compile_opt strictarr 17 17 18 define = { indgen_uts, inherits MG testSuite }18 define = { indgen_uts, inherits MGutTestSuite } 19 19 end -
trunk/mgunit/docs/indgen_ut__define.pro
r49 r50 10 10 11 11 ;+ 12 ; This test should pass the assertion and return 1 (i.e. success). Tests can 13 ; also return 0 or generate an error to indicate failure. 14 ;- 15 function indgen_ut::test_basic 16 compile_opt strictarr 17 18 a = indgen(5) 19 assert, array_equal(a, [0.0, 1.0, 2.0, 3.0, 4.0]), 'Correct elements' 20 21 return, 1 22 end 23 24 25 ;+ 12 26 ; This test fails because the assertion is wrong. 13 27 ;- 14 function indgen_ut::test 128 function indgen_ut::test_fail_example 15 29 compile_opt strictarr 16 30 … … 23 37 24 38 ;+ 25 ; This test should pass the assertion and return 1 (i.e. success). Tests can26 ; also return 0 or generate an error to indicate failure.27 ;-28 function indgen_ut::test229 compile_opt strictarr30 31 a = indgen(5)32 assert, array_equal(a, [0.0, 1.0, 2.0, 3.0, 4.0]), 'Correct elements'33 34 return, 135 end36 37 38 ;+39 39 ; This is a test that will pass because the code of the test is supposed to 40 40 ; cause an error. To do this kind of test, use the "error_is_pass" batch file. 41 41 ;- 42 function indgen_ut::test 342 function indgen_ut::test_error 43 43 compile_opt strictarr 44 45 44 @error_is_pass 46 45 … … 55 54 ; "error_is_fail" batch file. IO errors don't normally cause a test to fail. 56 55 ;- 57 function indgen_ut::test 456 function indgen_ut::test_baderror 58 57 compile_opt strictarr 58 @error_is_fail 59 59 60 @error_is_fail61 62 60 a = indgen('another_string') 63 61 … … 72 70 compile_opt strictarr 73 71 74 define = { indgen_ut, inherits MG testCase }72 define = { indgen_ut, inherits MGutTestCase } 75 73 end -
trunk/mgunit/docs/using-mgunit.txt
r49 r50 1 MGunit is a unit testing framework modeled on other unit testing frameworks such as JUnit. The goal is to allow easy creation and reporting of results of tests, but still allow for all needed flexibility. 1 Introduction 2 ------------ 2 3 3 The structure of tests created for this framework is straightforward. Individual tests are methods of an class that subclasses MGtestCase. Each method returns 1 for success or 0 for failure (or throws an error). Each test method's name must start with "test". Test cases may be (but are not required to be) grouped into test suites.4 MGunit is a unit testing framework modeled on other unit testing frameworks such as JUnit. The goal is to allow easy creation and reporting of results of tests, but still allow for all needed flexibility. Simple naming conventions replace formal creation of hierarchies and specification of tests. This allows test suites to be created with a minimum of code beyond the actual code of the tests themselves. 4 5 5 The examples (findgentest, indgentest, and indgensuite) of MGunit discussed are available for download.6 6 7 To create a test, subclass MGtestCase like:: 7 Basic use 8 --------- 8 9 9 pro findgentest__define 10 define = { findgentest, inherits MGtestCase } 10 Individual tests are methods of a class that subclasses MGutTestCase. Each method returns 1 for success or 0 (or throws an error) for failure. Each test method's name must start with "test". 11 12 For example, let's create some tests for the FINDGEN function. First, subclass MGutTestCase like below:: 13 14 pro findgen_ut__define 15 compile_opt strictarr 16 17 define = { findgen_ut, inherits MGutTestCase } 11 18 end 12 19 13 A test is just a method whose name starts with "test". MGunit will find the tests automatically (make sure they are either already compiled or above findgentest__define in the same file). A test case may have as many tests as necessary. For example, a simple test: 14 function findgentest::test2 15 a = findgen(5) 16 assert, array_equal(a, [0.0, 1.0, 2.0, 3.0, 4.0]), 'Correct elements' 20 A test is just a method of this class whose name starts with "test". The mgunit framework will find the tests automatically. For example, a simple test:: 17 21 18 return, 1 19 end 22 function findgen_ut::test_basic 23 compile_opt strictarr 24 25 a = findgen(5) 26 assert, array_equal(a, [0.0, 1.0, 2.0, 3.0, 4.0]), 'Correct elements' 20 27 21 Return 1 for success. For failure, either return 0 or throw an error. Here the helper routine ASSERT will throw an error using the given message if its condition is not met. This will be reported as a failure along with the message. To run this test, use: 22 IDL> mgunit, 'findgentest' 28 return, 1 29 end 30 31 Return 1 for success. For failure, either return 0 or throw an error. Here the helper routine ASSERT will throw an error using the given message if its condition is not met. This will be reported as a failure along with the message. To run this test, use the following:: 32 33 IDL> mgunit, 'findgen_ut' 34 "All tests" test suite starting (1 test suite/case, 1 tests) 35 "findgen_ut" test case starting (4 tests) 36 test_basic: passed 37 Results: 1 / 1 tests passed 38 Results: 1 / 1 tests passed 39 40 A test case may have as many individual tests (methods with names starting with "test") as necessary. 41 42 One tricky situation is that sometimes invalid input must be tested to make sure the routine fails. In these situations, throwing an error should indicate the success of the test, not a failure. In this case use the provided batch file error_is_pass at the beginning of the routine, like:: 43 44 function findgen_ut::test_error 45 compile_opt strictarr 46 @error_is_pass 47 48 a = findgen('string') 49 50 return, 0 51 end 52 53 As an example of showing a failing test, the example test case includes a test_fail_example method with an invalid assertion. Also provided is an example of using the error_is_fail batch file in the test_baderror method. Runtime errors will cause a test to fail, but IO errors normally will not. The test_baderror test uses @error_is_fail to make an IO error cause the test to fail:: 54 55 function findgen_ut::test_baderror 56 compile_opt strictarr 57 @error_is_fail 58 59 a = findgen('another_string') 60 61 return, 1 62 end 63 64 Running all the test case now results in the following:: 65 66 IDL> mgunit, 'findgen_ut' 67 "All tests" test suite starting (1 test suite/case, 4 tests) 68 "findgen_ut" test case starting (4 tests) 69 test_basic: passed 70 test_error: passed 71 test_fail_example: failed "Wrong number of elements" 72 test_baderror: failed "Type conversion error: Unable to convert given STRING to Long64." 73 Results: 2 / 4 tests passed 74 Results: 2 / 4 tests passed 75 76 Both test failures above are expected and present only to demonstrate features of mgunit. 77 78 79 Running multiple test cases 80 --------------------------- 81 82 Another test case, indgen_ut, is provided as an example. It is analogous to findgen_ut for the INDGEN routine. 83 84 Multiple test cases can be executed by specifying them as an array:: 85 86 IDL> mgunit, ['findgen_ut', 'indgen_ut'] 87 "All tests" test suite starting (2 test suites/cases, 10 tests) 88 "findgen_ut" test case starting (5 tests) 89 test_baderror: failed "Type conversion error: Unable to convert given STRING to Long64." 90 test_basic: passed 91 test_error: passed 92 test_fail_example: failed "Wrong number of elements" 93 test_incorrecterror: failed "Type conversion error: Unable to convert given STRING to Long64." 94 Results: 2 / 5 tests passed 95 "indgen_ut" test case starting (5 tests) 96 test4: failed "Type conversion error: Unable to convert given STRING to Long64." 97 test_baderror: failed "Type conversion error: Unable to convert given STRING to Long64." 98 test_basic: passed 99 test_error: passed 100 test_fail_example: failed "Wrong number of elements" 101 Results: 2 / 5 tests passed 102 Results: 4 / 10 tests passed 103 104 Alternatively, test cases may be grouped into test suites. Test suites are just collections of test cases. To make a suite, subclass MGutTestSuite and use the add method in the the subclass' init method to add test classes. For example, to make a suite containing the indgen_ut and findgen_ut test cases:: 105 106 function indgen_uts::init, _extra=e 107 compile_opt strictarr 108 109 if (~self->mguttestsuite::init(_extra=e)) then return, 0 110 111 ;self->add, ['indgen_ut', 'findgen_ut'] 112 self->add, /all 113 114 return, 1 115 end 116 117 pro indgen_uts__define 118 compile_opt strictarr 119 120 define = { indgen_uts, inherits MGutTestSuite } 121 end 122 123 The commented out line will specifically add indgen_ut and findgen_ut, wherever their source code files may be located. Instead, the ALL keyword is used to add all test cases in the same directory as the test suite source code file. Test cases to be found in this manner must use the convention to name the class with an appended "_ut", as in "findgen_ut". 124 125 mgunit will also accept a mixed array of test suites and test cases, as in:: 126 127 IDL> mgunit, ['findgen_ut', 'indgen_ut', 'indgen_uts'] 128 129 In our case, this does not make sense because this will execute the same tests twice. 130 131 132 Fixtures 133 -------- 134 135 Setup/teardown. 136 137 Common checks i.e. memory leaks 138 139 140 Other output 141 ------------ 142 143 Compound, log, HTML 23 144 24 145 Results can be sent to a log file with the LOG_FILE: … … 34 155 Results: 2 / 4 tests passed 35 156 Results: 2 / 4 tests passed 157 36 158 37 One tricky situation is that sometimes invalid input must be tested to make sure the routine fails. In this case, throwing an error should indicate a success of the test, not a failure. In this case use the provided batch file error_is_pass at the beginning of the routine, like: 38 function findgentest::test3 39 @error_is_pass 159 Miscellaneous 160 ------------- 40 161 41 a = findgen('string') 162 Templates for the IDL Workbench are provided to make test/suite creation even faster. To use them, first navigate to the Workbench preferences. There should be a Templates section under the IDL heading. Click the "Import" button on the right and navigate to the "test-templates.xml" file in the mgunit source. Two new templates, "Test case" and "Test suite", should now be available. Typing "testcase" into a new file and then selecting Edit > Content Assist from the menus will create a test case which can be filled out like a form. Suites can be created the same way by typing "testsuite". 42 163 43 return, 144 end45 164 46 Multiple test cases can be specified as an array 47 IDL> mgunit, ['findgentest', 'indgentest'] 165 Tips 166 ---- 48 167 49 or by creating a test suite. Test suites are very simple; they are just collections of test cases. To make a suite, subclass MGtestSuite and use the add method to add test classes. For example, 50 function indgensuite::init, _extra=e 51 compile_opt strictarr 168 Subclass a subclass of MGutTestCase. 52 169 53 if (~self->mgtestsuite::init(_extra=e)) then return, 054 55 self->add, ['indgentest', 'findgentest']56 57 return, 158 end59 60 pro indgensuite__define61 define = { indgensuite, inherits MGtestSuite }62 end63 64 This test suite is available for download (source / docs). mgunit can run test suites also,65 IDL> mgunit, 'indgensuite'66 67 mgunit will also accept a mixed array of test suites and test cases. -
trunk/mgunit/Makefile
r49 r50 33 33 cp src/error_is_pass.pro mgunit-$(RELEASE)/ 34 34 cp src/style.css mgunit-$(RELEASE)/ 35 cp src/ test-templates.xml mgunit-$(RELEASE)/35 cp src/mgunit-templates.xml mgunit-$(RELEASE)/ 36 36 cp COPYING mgunit-$(RELEASE)/ 37 37 cp RELEASE mgunit-$(RELEASE)/ … … 54 54 cp src/dist_tools/mg_src_root.pro mgunit-src-$(RELEASE)/ 55 55 cp src/cmdline_tools/mg_ansicode.pro mgunit-src-$(RELEASE)/ 56 cp src/ test-templates.xml mgunit-src-$(RELEASE)/56 cp src/mgunit-templates.xml mgunit-src-$(RELEASE)/ 57 57 cp COPYING mgunit-src-$(RELEASE)/ 58 58 cp RELEASE mgunit-src-$(RELEASE)/
