Data-driven unit tests for managed code

The Microsoft unit testing code for managed code allows to create test methods that are executed with data automatically fetched from an external data source. This is very helpful because we can extend the data sets without modifying the test code. An external data source can be an SQL database, a CSV file or Excel document, an XML document, or anything else for which a provider for .NET exists. In this article, I will show how you can define test data using all these types of source and execute unit testing methods with it.

This feature is available only for .NET Framework.
At this time, data-driven unit testing from a data source is not supported for .NET Core.

Unit testing non-public types and members for .NET projects

Unit testing is usually used for testing public types and members. However, there are cases when you might need to test types or members that are not public. These could be internal classes or private helper methods, for instance. Whether that is proper unit testing or beyond its scope is not a discussion that I want to get into here. However, in this post, I will show how you can unit test non-public types and members from .NET assemblies.

When faced with the need for testing non-public types and members you can use several approaches:

  • change the accessibility to public; you can do that perhaps only for debug builds and keep the intended accessibility in release builds by using conditional compilation.
  • provide public members of a class that invoke private ones;
  • use reflection.

The first solutions involve changing the API only for the sake of the testing. The last solution avoids that but requires more work. To help with that, the Visual Studio unit testing framework provides some helper types that enable you to focus on the actual testing and be less concerned about the reflection details.

Writing C++ unit tests with Catch2

In my book, Modern C++ Programming Cookbook, I discussed several testing frameworks for C++, more precisely, Boost.Test, Google Test, and Catch (which stands for C++ Automated Test Cases in a Header). Since the publishing of the book, a new version of Catch, called Catch2 has been released. This provides new functionalities, but also a series of breaking changes with Catch, including the drop of support for pre-C++11 compilers. For a list of changes, you can see the Catch2 release notes. Catch2 is available as a single-header library, is open-sources and cross-platform, and written for C++11/14/17/latest. In this article, I want to give a brief example of how you can write tests for C++ using Catch2.

TDD with Live Unit Testing in Visual Studio 2017

Visual Studio 2017 Enterprise provides a feature called Live Unit Testing that enables developers to see live how changing C# and VB.NET code affects its corresponding unit tests. Among its features, it includes showing coverage information in the editor as you type, integration with the Test Explorer, including/excluding targeted test methods or projects for large…