Introduction
In this article, we will explore the power of DotNetFiddle, an online C# and VB.NET compiler and executor with an extensive .NET Framework and .NET Core library support. We will focus on how to create unit tests to demonstrate behavior, and this will provide valuable insights into how you can use DotNetFiddle for your next project.
What are Unit Tests?
Unit tests are automated tests created to validate the behavior of individual components (units) of a software application. Typically, unit tests are written and executed in isolation from the rest of the codebase, ensuring each function or method returns the expected value under specific conditions.
Benefits of Using Unit Tests
Unit testing provides numerous benefits throughout the software development lifecycle, including improved code quality, maintainability, and faster development by enabling:
- Early error detection
- Decoupling the system for simpler testing
- Refactoring with confidence
- Reduced integration issues
Getting Started with DotNetFiddle Unit Tests
DotNetFiddle offers a convenient way to write and execute unit tests without requiring a local development environment. Let's understand how to create a Fiddle with a class library project and unit tests.
Creating a Class Library Project in DotNetFiddle
To start, open DotNetFiddle and select the C# tab. Choose the desired .NET Framework or .NET Core version and leave the output type as "Console App" for now. Once the editor is available, let's write the class library code:
namespace MyLibrary
{
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
}Setting Up a Test Project
To create a separate test project, we'll use MSTest, a popular unit testing framework included in .NET. First, let's create a new Fiddle for the test project by clicking "+New Fiddle" at the top-right corner. Now, select the C# tab again, choose the same .NET Framework or .NET Core version, and change the output type to "Class Library."
Add the following code to reference the class library project and include the MSTest framework:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyLibrary;
[assembly: TestAssembly("MyLibrary.Tests", Platform targetFramework=".NETFramework,Version=v4.8")]
[assembly: TestFixtureSource(typeof(TestFixtureGenerator))]
namespace MyLibrary.Tests
{
public class TestFixtureGenerator
{
[AssemblyInitialize]
public static void InitializeAssembly(TestContext context)
{
// Optionally perform one-time assembly initialization here
}
}
}Writing Unit Tests
Now, let's write a unit test for the Calculator.Add method:
[TestClass]
public class CalculatorTests
{
[TestMethod]
public void TestMethodAdd()
{
Calculator calculator = new Calculator();
int result = calculator.Add(3, 5);
Assert.AreEqual(8, result, "Add returned incorrect result");
}
}
Running Unit Tests in DotNetFiddle
After writing the unit tests, let's execute them. However, since we've structured our Fiddles as two individual projects, we need to combine them for testing. Fortunately, DotNetFiddle's snapshot feature allows us to do this seamlessly.
Save both Fiddles and click on the camera icon at the top-right corner of each Fiddle to create a snapshot.
Once the snapshots are generated, copy the content from the second Fiddle (MyLibrary.Tests) and replace the contents of the first Fiddle (MyLibrary).
Now, click the "Run" button at the top. The output will show if any tests have failed and the corresponding error message:
------ Test started ------
Test method MyLibrary.Tests.CalculatorTests.TestMethodAdd test passed.
Test Run Successful.
Total tests: 1
Passed: 1
Failed: 0
!message : Open https://dotnetfiddle.net/V9XM1o to view the test results (test kind: MSTest)
In this article, we demonstrated the use of DotNetFiddle for unit testing C# and VB.NET projects. We've learned how to:
- Create a class library project in DotNetFiddle
- Set up a test project
- Write unit tests with MSTest
- Run and view test results in DotNetFiddle