· Oct 20, 2016

%UnitTest Code Coverage


When we write unit test cases for cache object script code using %UnitTest.TestCase, what  is the best way to write code to identify code coverage?

So, let say my unit test case hit all 10 lines of code of a method for a given class. So, unit test coverage should be 100% for that. But, using line-by-line coverage [(%Monitor.System.LineByLine] getting wrong percentage, because it also includes code comment/documentation as part of code. So, practically we can not ever achieve 100% of code coverage by using this API.

I'm not sure, if am able to describe the problem properly here. Please let me know if you need any further details. I can try to elaborate a bit more for the same.

Is there any easy solution/best practices to find code coverage using some plugin or available classes? How people do it usually?



Discussion (2)1
Log in or sign up to continue

The best tool I've seen for tracking unit test coverage in Caché ObjectScript is - maybe you'd already found that from some old Developer Community posts. However, it's gone stale, it's not really mature/complete, and it doesn't have a solution for the problem you've described. There are further complications because %Monitor.System.LineByLine looks at the generated (.int) code, which contains code that isn't in the class definition (because it's generated) and may not contain code that is in the class (for example, a classmethod that returns a constant). It also looks at code line-by-line, and there may be multiple statements on a line; tracing with ZBREAK instead could be a solution for this.

It's worth noting that #; comments don't appear in .int code - so, if only this type of comment is used, you could accurately measure the percentage of code coverage for a method/classmethod as the percentage of code coverage of the generated .int code corresponding to the method/classmethod. Otherwise, you're stuck parsing the code (which, if you're just trying to detect comments, wouldn't be too bad) to detect lines that contain comments and omit them from consideration when determining code coverage percentage.

Thanks for your response. I looked into the github utcov. But still, the entire approach is w.r.t the INT code rather than the actual lines of code in classes. Also, when you have thousands of classes running in production for many clients, its difficult to even change the comment with #;. That's a huge effort though. But, adding the unit test cases even for a old code is like covering the tech debt, and that's a ongoing practice.

Having said that, I was really looking for something easy solution like any other language has. May be once we star using Atelier, there could be some addin or frameworks to use with Cache Object Scripts.