Creating unit test method stubs with “Create Unit Tests”
In Visual Studio 2015 CTP 6 we are introducing the “Create Unit Tests” feature that provides the ability to create unit test method stubs. The feature allows easy configuration of a test project, and the test class and the test method stub therein. It is conveniently available as a context menu item, and can be invoked on product code at the scope of a method, a type, or a namespace. It launches a fairly self-explanatory dialog that surfaces the options that can be configured (does the look and feel seem familiar to you?). Support is presently for C# and the MSTest framework but enhancements are in the pipeline.
This addresses a popular “ask” on user voice from the community.
If we take a step back and look at the purpose that a unit test serves, then this “ask” underscores the diversity of unit testing approaches. A unit test serves as an executable specification representing the intended behaviour of the code, for some chosen test values. When such a test fails it means that the code does not do what it’s supposed to do. Some testing practices grow the code by building such a specification ground up, starting from a failing test case. A practitioner might approach this situation by choosing to start from the unit test template available through the Visual Studio project system.
But there are situations that may not readily afford this practice. Consider existing code with very little to no test coverage, and no documentation either – i.e. where there is limited or non-existent specifications. Here, fully baked code serves as the starting point, and the tests need to be built ground up. For this situation, an approach might be to bring to bear the intelligence of Smart Unit Tests and have it generate a suite of tests characterizing the observed behaviour of the code.
Then there is the situation where the developer starts by writing some code, and uses that to boot strap the unit testing discipline. Within the flow of coding, the developer might want to quickly create a unit test method stub (with a suitable test class, and a suitable test project) for a particular piece of code. The use of the “Create Unit Tests” feature might be the approach in this situation.
Much as with coding, there are different approaches to unit testing as well – varying from that of the purist to that of the pragmatist – from “test first” to “code first” to those who might want the flexibility to start in either of the ways. We see “Create Unit Tests” as an important addition to the complement of features we have for supporting these various approaches. We hope you find this feature, and indeed the other features you are seeing us introduce in the unit testing space, to your liking.
As ever, please submit bugs through Connect, suggestions on Uservoice, and quick thoughts via Send-a-Smile in the Visual Studio IDE. We look forward to your feedback.