|
1 |
| -# TestCoverage |
2 |
| -Test Coverage Tool |
| 1 | +# Unit Test Coverage for ObjectScript |
| 2 | + |
| 3 | +Run your typical ObjectScript %UnitTest tests and see which lines of your code are executed. Includes Cobertura-style reporting for use in continuous integration tools. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +### Installation: from Terminal |
| 8 | + |
| 9 | +First, clone or download the repository. Then run the following commands: |
| 10 | + |
| 11 | +``` |
| 12 | +Set root = "<path on filesystem to which repository was cloned/downloaded>" |
| 13 | +Do $System.OBJ.ImportDir(root,"*.inc;*.cls","ck",,1) |
| 14 | +``` |
| 15 | + |
| 16 | +### Installation: from Atelier |
| 17 | + |
| 18 | +Note: this assumes you have the Git plugin for Atelier installed. |
| 19 | + |
| 20 | +1. Right-click in the Atelier Explorer and select Import... |
| 21 | +2. Select the "Projects from Git" wizard under the "Git" folder, then click "Next" |
| 22 | +3. In the "Select Repository Source" dialog, select "Clone URI" and enter this repository's URI, then click "Next" |
| 23 | +4. Select the branch(es) you intend to build from, then click "Next" |
| 24 | +5. Select a local destination in which to store the code, then click "Next" |
| 25 | +6. Choose "import existing Eclipse projects" and select the TestCoverage project, then click "Next" |
| 26 | +7. Configure a connection for the project |
| 27 | +8. Synchronize the project with your selected namespace |
| 28 | + |
| 29 | +### Security |
| 30 | +Note that, depending on your security settings, SQL privileges may be required for access to test coverage data. The relevant permissions may be granted by running: |
| 31 | + |
| 32 | +``` |
| 33 | +zw ##class(TestCoverage.Utils).GrantSQLReadPermissions("<username or role that should have read permissions>") |
| 34 | +``` |
| 35 | + |
| 36 | +For example: |
| 37 | + |
| 38 | +``` |
| 39 | +zw ##class(TestCoverage.Utils).GrantSQLReadPermissions("_PUBLIC") |
| 40 | +``` |
| 41 | + |
| 42 | +## User Guide |
| 43 | + |
| 44 | +### Running Tests with Coverage |
| 45 | +Generally speaking, set `^UnitTestRoot`, and then call `##class(TestCoverage.Manager).RunTest()` the same you would call `##class(%UnitTest.Manager).RunTest()`. For more information on InterSystems' %UnitTest framework, see the [tutorial](https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=TUNT) and/or the [class reference for %UnitTest.Manager](https://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?PAGE=CLASS&LIBRARY=%25SYS&CLASSNAME=%25UnitTest.Manager). |
| 46 | + |
| 47 | +The "userparam" argument can be used to pass information about code coverage data collection. For example: |
| 48 | + |
| 49 | +``` |
| 50 | +Set tCoverageParams("CoverageClasses") = <$ListBuild list of class names for which code coverage data should be collected> |
| 51 | +Set tCoverageParams("CoverageRoutines") = <$ListBuild list of routine names for which code coverage data should be collected> |
| 52 | +Set tCoverageParams("CoverageDetail") = <0 to track code coverage overall; 1 to track it per test suite (the default); 2 to track it per test class; 3 to track it per test method.> |
| 53 | +Do ##class(TestCoverage.Manager).RunTest(,,.tCoverageParams) |
| 54 | +``` |
| 55 | + |
| 56 | +The first two arguments to `TestCoverage.Manager:RunTest` are the same as `%UnitTest.Manager`. |
| 57 | + |
| 58 | +At the selected level of granularity (before all tests or a test suite/case/method is run), there will be a search for a file named "coverage.list" within the directory for the test suite and parent directories, stopping at the first such file found. This file may contain a list of classes, packages, and routines for which code coverage will be measured. For .MAC routines only (not classes/packages), the coverage list also supports the * wildcard. It is also possible to exclude classes/packages by prefixing the line with "-". For example, to track coverage for all classes in the `MyApplication` package (except those in the `MyApplication.UI` subpackage), and all routines with names starting with "MyApplication": |
| 59 | + |
| 60 | +``` |
| 61 | +// Include all application code |
| 62 | +MyApplication.PKG |
| 63 | +MyApplication*.MAC |
| 64 | +
|
| 65 | +// Exclude Zen Pages |
| 66 | +-MyApplication.UI.PKG |
| 67 | +``` |
| 68 | + |
| 69 | +As an alternative approach, with unit test classes that have already been loaded and compiled (and which will not be deleted after running tests) and a known list of classes and routines for which code coverage should be collected, use: |
| 70 | + |
| 71 | +``` |
| 72 | +Do ##class(TestCoverage.Manager).RunAllTests(tPackage,tLogFile,tCoverageClasses,tCoverageRoutines,tCoverageLevel,.tLogIndex,tSourceNamespace,tProcessIDs,tTiming) |
| 73 | +``` |
| 74 | + |
| 75 | +Where: |
| 76 | + |
| 77 | +* `tPackage` has the top-level package containing all the unit test classes to run. These must already be loaded. |
| 78 | +* `tLogFile` (optional) may specify a file to log all output to. |
| 79 | +* `tCoverageClasses` (optional) has a $ListBuild list of class names within which to track code coverage. By default, none are tracked. |
| 80 | +* `tCoverageRoutines` (optional) has a $ListBuild list of routine names within which to track code coverage. By default, none are tracked. |
| 81 | +* `tCoverageLevel` (optional) is 0 to track code coverage overall; 1 to track it per test suite (the default); 2 to track it per test class; 3 to track it per test method. |
| 82 | +* `tLogIndex` (optional) allows for aggregation of code coverage results across unit test runs. To use this, get it back as output from the first test run, then pass it to the next. |
| 83 | +* `tSourceNamespace` (optional) specifies the namespace in which classes were compiled, defaulting to the current namespace. This may be required to retrieve some metadata. |
| 84 | +* `tPIDList` (optional) has a $ListBuild list of process IDs to monitor. If this is empty, all processes are monitored. By default, only the current process is monitored. |
| 85 | +* `tTiming` (optional) is 1 to capture execution time data for monitored classes/routines as well, or 0 (the default) to not capture this data. |
| 86 | + |
| 87 | +### Viewing Results |
| 88 | +After running the tests, a URL is shown in the output at which you can view test coverage results. |
| 89 | + |
| 90 | +### Reporting on results in Cobertura format |
| 91 | +The `RunTest()` method reports back a log index in the "userparam" argument. This can be used to generate a report in the same format as Cobertura, a popular Java code coverage tool. For example: |
| 92 | + |
| 93 | +``` |
| 94 | +Set userParams("CoverageDetail") = 0 |
| 95 | +Do ##class(TestCoverage.Manager).RunTest(,"/nodelete",.userParams) |
| 96 | +Set reportFile = "C:\Temp\Reports\"_tUserParams("LogIndex")_"\coverage.xml" |
| 97 | +Do ##class(TestCoverage.Report.Cobertura.ReportGenerator).GenerateReport(userParams("LogIndex"),reportFile) |
| 98 | +``` |
| 99 | + |
| 100 | +This exports both the coverage results themselves and the associated source code (in UDL format) for correlation/display, and has been verified with [the Cobertura plugin for Jenkins](https://wiki.jenkins.io/display/JENKINS/Cobertura+Plugin). |
| 101 | + |
| 102 | +## Support |
| 103 | + |
| 104 | +If you find a bug or would like to request an enhancement, [report an issue](/issues/new). If you have a question, post it on the [InterSystems Developer Community](https://community.intersystems.com/) - consider using the "Testing" or "Continuous Integration" tags as appropriate. |
| 105 | + |
| 106 | +## Contributing |
| 107 | + |
| 108 | +Please read [contributing](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us. |
| 109 | + |
| 110 | +## Versioning |
| 111 | + |
| 112 | +We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags). |
| 113 | + |
| 114 | +## Authors |
| 115 | + |
| 116 | +* **Tim Leavitt** - *Initial implementation* - [timleavitt](http://github.com/timleavitt) |
| 117 | + |
| 118 | +See also the list of [contributors](/contributors) who participated in this project. |
| 119 | + |
| 120 | +## License |
| 121 | + |
| 122 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. |
0 commit comments