Feb 25, 2020 9:00:00 AM by Mathew Phan
In my previous blog post, I talked about what McCabe's Cyclomatic Complexity is, how to calculate it, and how to use Toad® for Oracle to calculate it for you.
Now that you understand that McCabe's Cyclomatic Complexity is a representation of how complex your code is, and you understand that you should work to reduce the value, what do you do next—after you've done your best to reduce it as much as possible? You can use McCabe’s Cyclomatic Complexity to design unit tests in Toad for Oracle.
It is best practice to have a test case for each pathway in you code. In other words, since I have a calculated complexity of 3, I should have at least 3 test cases to ensure proper code coverage.
Let's review my previous code example. I've commented inline the three possible pathways. [I've also modified my code a bit from my previous blog post to cover positive and negative decimal values just in case]:
So let's take a look at the 3 possible scenarios.
In order to ensure proper code coverage, we should create a test case for each scenario. Fortunately Toad 13.2 comes with an easy way to do this. Toad 13.2 integrates with utPLSQL framework. If you haven't heard about this, you can learn more here.
I modify the code to set l_INVAL to 10:
Best of all, the work I did in creating these tests, is saved to my database. I can always re-run these tests as needed. If the code ever changes and I need to do regression testing, all my inputs and expected outputs are saved and I can just run them all with a single click. Other people who have access can run this package and run my tests as well, and this package can be called from other tools that can run PLSQL too.
I hoped you enjoyed this two-part blog series on McCabe's Cyclomatic Complexity and unit testing. And, I hope you test your code!
Have you heard about the new Toad utPLSQL unit test creation module?
Most developers and DBAs use Toad for Oracle to reduce time and effort to develop and manage Oracle databases. But did you know that the newly released Toad for Oracle 13.2 includes a new utPLSQL unit test creation module—and it’s available in all Toad editions Nov 18.
With this new utPLSQL feature, developers can create unit tests easily using an industry standard approach without leaving their database development tool of choice.
What else can Toad do that you didn’t know about?
Already in a trial? Talk to sales or buy now online.
Already a loyal fan of Toad for Oracle? Renew now.
Blog: More blogs from Mat.
Have questions, comments, or an idea for a new feature? Interact with experts on the Toad for Oracle forum on Toad World®.
If you think your colleagues would benefit from this blog, share it now on social media with the buttons located at the top of this blog post. Thanks!
Written by Mathew Phan
Mathew Phan began his career as a technical support engineer for Toad for Oracle soon after graduating from the University of California, Irvine, where he obtained a bachelor's degree in computer and information science. Mat has devoted his entire 14-year career with the Toad family of database solutions and held various positions helping to shape the direction and growth of Toad. Early in his tenure, his focus was on Oracle databases from a developer point of view. Now as a systems consultant, he continues to work with customers to build effective solutions that meet their needs.