This blog will investigate troubleshooting TestBox Code coverage using FusionReactor.
What is TestBox?
TestBox is a robust testing framework for ColdFusion (CFML) that many developers rely on for producing both unit and behavior-driven (BDD) tests for their web applications.
What is Code Coverage?
Code Coverage is a metric that provides the percentage of your source code which is included in the tests that you write. If your code coverage score is too low, it is usually an indicator that you must write more tests. Many developers rely on code coverage statistics to help determine how complete their tests are.
I recently ran into issues with code coverage reporting while running tests for a ColdBox application I was developing for a client. My tests were executed successfully, but the code coverage statistics weren’t updating, and the information displayed didn’t reflect my actual code. The summary results showed files from the ColdBox framework which should be ignored by default.
I put together some troubleshooting tips based on my own experimentation and the assistance I received from Brad Wood on the Ortus Community Forums. The forums are a great resource for anyone looking to discuss ColdBox, TestBox, and more.
Troubleshooting TestBox Code coverage Tips:
Make sure your /tests/ folder is fully up to date with the latest version. When you run package update in Commandbox, it updates the framework and any dependencies you have installed, but it will not touch the files in your /tests/ folder, which includes the TestBox Browser. Therefore, I recommend visiting the latest version of the ColdBox advanced script template to make sure your version is up to date. If you replace your copy of /tests/Application.cfc, make sure to reconfigure it for your testing needs. For example, you may need to include your data source and/or ORM settings. Also, be careful not to overwrite the /tests/specs/ directory because that is where your tests live.
Code Coverage only works if you have a valid FusionReactor License. Type fr open in Commandbox and log into FusionReactor, you should see a green message on the screen that indicates your license is active. If you do not have a license, I recommend picking up a Developer license subscription or a free trial license.
If you run your tests using the “Run All” button from the TestBox Browser located at http://[your host]/tests/, Code Coverage will not execute and will instead show inaccurate code coverage statistics. Code Coverage statistics will also not show if you run individual “a-la-carte” tests. Code coverage will only generate if you execute all of your tests via the URL http://[your host]/tests/runner.cfm.
Make sure to delete the /tests/results/coverageReport folder as an added precaution.
You will know when the coverage report is generated properly when you see the “Coverage Browser generated in…” message on the result screen, and you see your own app’s files listed in the “Files with best coverage” section.
Hopefully, these tips will help you in your journey using code coverage in your tests. If you have other tips for troubleshooting code coverage with TestBox and FusionReactor, please feel free to submit them in the comments.