Introduction to Automation Testing
Explore how to implement automation testing for AWS Lambda functions using .NET testing frameworks such as xUnit, NUnit, and MSTest. Understand how to create test projects, define test cases, write both parameterized and non-parameterized tests, and run them efficiently to validate Lambda function behavior.
Automation testing is a technique that allows us to test the code of our application without having to build the entire application and validate its behavior manually. It can be applied to any application type, and AWS Lambda applications are not an exception.
How automation testing works on .NET
There are three main testing frameworks supported by .NET:
xUnit
NUnit
MSTest
Although these frameworks use different ways of writing tests, and each has its pros and cons, the general principles of how they work are the same. The process of writing tests will go through the following stages:
Create a test project: We need to create a project that will contain our test methods. This will always be separate from the project we’re testing. The test project will have all the necessary NuGet package references that will allow us to write tests.
Add references to the projects to be tested: The test project needs information on the project it’s testing, so the project under test (i.e., our application or a library) needs to be added to the test project as a reference.
Define the test cases: Our tests need to be meaningful. We need to test scenarios that validate the behavior of various components.
Write the test methods: Once we know what we’re testing, we need to write special test methods. These methods will be marked with special attributes that will make them recognizable as tests by a test runner. The exact attributes vary among testing frameworks.
Let’s take a look at an example of how these steps can be applied. For demonstration purposes, we will be using xUnit.
Example of xUnit usage
In the following playground, we have a basic .NET console application represented by the MainApp project. Within this project, we have the Calculator.cs file. The Calculator class in the file has the MultiplyByTwo method, which accepts an integer parameter, multiplies it by 2, and returns the result.
namespace MainApp;
public class Calculator
{
public int MultiplyByTwo(int value)
{
return value * 2;
}
}
The MainApp.Tests project also provides some unit tests. We can open the MainApp.Tests.csproj file to see what dependencies we need for unit testing. The required dependencies are found on lines 13–22:
Microsoft.NET.Test.Sdk: Allows the tests to be executed by .NET.xunit: Contains the core xUnit dependencies.xunit.runner.visualstudio: Makes xUnit tests recognizable by Test Explorer in Visual Studio .IDE Integrated development environment. A software that allows developers to write code, debug it, and build the application. coverlet.collector: Allows the tests to collect code coverage metrics.
Once we have installed all the required dependencies, we can start writing tests.
Writing xUnit tests
All our test methods in the example can be found in the CalculatorTests.cs file. This file has two test methods:
The
MultiplyByTwo_CanMultiplymethod on line 6. This is an example of a simple test method without parameters.The
MultiplyByTwo_CanMultiplyAnyNumbermethod on line 18. This is an example of a test method with parameters.
Let’s examine what makes these methods different.
Nonparameterized method
In xUnit simple methods without parameters are marked with the Fact attribute on line 5. Each of these methods represents a single scenario. In this example, we’re performing the following actions in this method:
Creating an instance of the
Calculatorclass.Passing
2as the parameter to theMultiplyByTwomethod and retrieving the result.Verifying that the result is equal to
4.
The verification is performed by using the Assert static class found on line 10. If the numbers don’t match, the test will fail. It will also fail if an exception is thrown during the run.
Parameterized tests
Parameterized test methods are marked with the Theory attribute that we see on line 13. We also have multiple InlineData attributes, such as the one found on line 14. This attribute passes the parameters to the method. The values we pass to these attributes must match the quantity and data types of the parameters the method accepts. In our case, we have two parameters, and both of them are int.
In this setup, each instance of the InlineData attribute represents a single test scenario. This way, we can avoid code duplications. If we know what output would be expected for each input, we can just pass those as parameters. We can also include
Running the tests
To see the results of the test, all we need to do is click the “Run” button in the playground. This will build both projects and then execute all available tests by using the dotnet test command-line interface (CLI) command. The results of the tests will be displayed in the console. If all tests passed successfully, the results will be displayed in green.