Posts

Showing posts with the label Visual Studio

Writing unit tests with GitHub Copilot

Image
Introduction A lot of developers do not like to write unit tests. Generative AI tools like GitHub Copilot can help you write unit tests. At least that is what these tools promise. In this blog post, I will write some unit tests using Github Copilot (with Chat) for a simple example to check if the promise is kept and what kind of changes to the generated code I have to make as a developer to get decent unit tests. Example For this blog post, I will use a probably familiar example of order and orderline items.  First tests  This code does not have a lot of logic to test. So let's add some logic to the order class. I ask GitHub Copilot to generate the code for calculating the total price for an order. In this code, I also changed the public list into a private one.   I can now ask Copilot to generate a unit test for this code using the prompt: "Can you generate a xunit test for the order and orderlineitem class where the order has 2 orderlineitems". Three unit tests were gen...

Better assertions for your unit tests

Image
This article is part of the  C# Advent calendar . During the event, two new articles are posted each day by authors throughout the community. Introduction Unlike many other developers, I do like unit tests. These tests have saved my professional life more than once. Without them, I would have pushed very bad mistakes to the remote codebase. As much as I like unit tests, I don't like the way assertions work in the unit testing frameworks in both xUnit and MSTest. In my opinion, the assertions do not express clearly what I want to assert and I also have to explain the correct order of arguments for Assert to new developers over and over again. For a couple of years, I started using another component for this: Fluent Assertions . In this blog post, I will show you how you can create more readable assertions using this package. Installation In order the start using the package, you have to install it from the NuGet-repository.  ...