Changes between Version 1 and Version 2 of Using
- Timestamp:
- 10/17/11 15:04:07 (20 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Using
v1 v2 7 7 8 8 9 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".9 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". 10 10 11 For example, let's create some tests for the FINDGEN function. First, subclass MGutTestCaselike below:11 For example, let's create some tests for the `FINDGEN` function. First, subclass `MGutTestCase` like below: 12 12 {{{ 13 13 pro findgen_ut__define … … 28 28 end 29 29 }}} 30 Return 1 for success. For failure, either return 0 or throw an error. Here the helper routine ASSERTwill 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:30 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: 31 31 {{{ 32 32 IDL> mgunit, 'findgen_ut' … … 39 39 A test case may have as many individual tests (methods with names starting with "test") as necessary. 40 40 41 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_passat the beginning of the routine, like:41 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: 42 42 {{{ 43 43 function findgen_ut::test_error … … 50 50 end 51 51 }}} 52 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_failto make an IO error cause the test to fail:52 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: 53 53 {{{ 54 54 function findgen_ut::test_baderror … … 75 75 Both test failures above are expected and present only to demonstrate features of mgunit. 76 76 77 A single test method of a test case can be run using a `.` to separate the test class name from the method name:: 78 {{{ 79 IDL> mgunit, 'findgen_ut.test_basic' 80 "All tests" test suite starting (1 test suite/case, 1 test) 81 "findgen_ut" test case starting (1 test) 82 test_basic: passed (0.000078 seconds) 83 Results: 1 / 1 tests passed, 0 skipped 84 Results: 1 / 1 tests passed, 0 skipped 85 }}} 86 77 87 78 88 == Running multiple test cases == 79 89 80 Another test case, indgen_ut, is provided as an example. It is analogous to findgen_ut for the INDGENroutine.90 Another test case, `indgen_ut`, is provided as an example. It is analogous to `findgen_ut` for the `INDGEN` routine. 81 91 82 92 Multiple test cases can be executed by specifying them as an array: … … 100 110 Results: 4 / 10 tests passed 101 111 }}} 102 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_uttest cases:112 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: 103 113 {{{ 104 114 function indgen_uts::init, _extra=e … … 119 129 end 120 130 }}} 121 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".131 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". 122 132 123 133 mgunit will also accept a mixed array of test suites and test cases, as in: … … 130 140 == Fixtures == 131 141 132 The setup and teardown methods of a test case class are executed before and after each individual test. By default, they are empty, but subclasses of MGutTestCasecan override them to do any common setup/teardown tasks. Any data to be stored from the setup is normally saved as an instance variable of the test case class so that it can be accessed by the test and the teardown method.142 The `setup` and `teardown` methods of a test case class are executed before and after each individual test. By default, they are empty, but subclasses of `MGutTestCase` can override them to do any common setup/teardown tasks. Any data to be stored from the setup is normally saved as an instance variable of the test case class so that it can be accessed by the test and the teardown method. 133 143 134 144 Pointer and object memory leaks can be tested for using fixtures by comparing the number of current pointers and objects during setup and teardown. … … 137 147 == Other output == 138 148 139 Results can be sent to a log file with the FILENAMEkeyword:149 Results can be sent to a log file with the `FILENAME` keyword: 140 150 {{{ 141 151 IDL> mgunit, 'indgen_uts', filename='test-results.log' … … 143 153 This will send the normal output to the results.log file. 144 154 145 HTML output can also be created with the boolean HTML keyword to the MGUNIT routine. Generally, the FILENAMEkeyword is used in conjunction with this option:155 HTML output can also be created with the boolean `HTML` keyword to the `MGUNIT` routine. Generally, the `FILENAME` keyword is used in conjunction with this option: 146 156 {{{ 147 157 IDL> mgunit, 'indgen_uts', filename='test-results.html', /html 148 158 }}} 149 159 160 150 161 == Miscellaneous == 151 162 152 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 Assistfrom 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".163 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". 153 164 154 165 … … 157 168 It can be useful to create a subclass of MGutTestCase for a project so that each test case in the project inherits from that class instead of directly from MGutTestCase. This case can do work common to all the tests i.e. find the location of test data, have common setup/teardown methods, etc. 158 169 159 The NTESTS, NPASS, and NFAIL keywords to the MGUNITroutine output the appropriate values. These can be handy for automated scripts i.e. sending email if any test fails, etc.170 The `NTESTS`, `NPASS`, and `NFAIL` keywords to the `MGUNIT` routine output the appropriate values. These can be handy for automated scripts i.e. sending email if any test fails, etc. 160 171
