Said's Blog

Types of software testing

September 4, 20235 min read
image

Software testing is an essential part of the software development process. There are various types of software testing, each serving a specific purpose. Here are some common types of software testing in C# with examples for each:

Unit testing

Unit tests are very low level and close to the source of an application. They consist in testing individual methods and functions of the classes, components, or modules used by your software. Unit tests are generally quite cheap to automate and can run very quickly by a continuous integration server.

Example
						//Testing a simple math function
[TestMethod]
public void TestAddition()
{
    Calculator calculator = new Calculator();
    int result = calculator.Add(2, 3);
    Assert.AreEqual(5, result);
}

					

Integration testing

Integration tests verify that different modules or services used by your application work well together. For example, it can be testing the interaction with the database or making sure that microservices work together as expected. These types of tests are more expensive to run as they require multiple parts of the application to be up and running.

Example
						//Verifying that data is correctly stored and retrieved
[TestMethod]
public void TestDatabaseIntegration()
{
    DatabaseManager dbManager = new DatabaseManager();
    bool isDataStored = dbManager.StoreData("Test Data");
    string retrievedData = dbManager.RetrieveData();
    Assert.IsTrue(isDataStored);
    Assert.AreEqual("Test Data", retrievedData);
}

					

Functional testing

Functional tests focus on the business requirements of an application. They only verify the output of an action and do not check the intermediate states of the system when performing that action.

There is sometimes a confusion between integration tests and functional tests as they both require multiple components to interact with each other. The difference is that an integration test may simply verify that you can query the database while a functional test would expect to get a specific value from the database as defined by the product requirements.

Example
						//Testing login functionality
[TestMethod]
public void TestLoginFunctionality()
{
    WebApplication app = new WebApplication();
    app.NavigateToLoginPage();
    app.EnterCredentials("username", "password");
    app.ClickLoginButton();
    bool isLoggedIn = app.IsUserLoggedIn();
    Assert.IsTrue(isLoggedIn);
}

					

Performance testing

Performance tests evaluate how a system performs under a particular workload. These tests help to measure the reliability, speed, scalability, and responsiveness of an application. For instance, a performance test can observe response times when executing a high number of requests, or determine how a system behaves with a significant amount of data. It can determine if an application meets performance requirements, locate bottlenecks, measure stability during peak traffic, and more.

Example
						[TestClass]
public class PerformanceTests
{
    [TestMethod]
    [Timeout(1000)] // Set a timeout for the test in milliseconds
    public void TestPerformance()
    {
        // Write code to simulate a performance test scenario
        // Measure the time it takes for an operation to complete
        // Assert that it is within acceptable performance limits
    }
}

					

Smoke testing

Smoke tests are basic tests that check the basic functionality of an application. They are meant to be quick to execute, and their goal is to give you the assurance that the major features of your system are working as expected.

Smoke tests can be useful right after a new build is made to decide whether or not you can run more expensive tests, or right after a deployment to make sure that they application is running properly in the newly deployed environment.

Example
						[TestClass]
public class SmokeTests
{
    [TestMethod]
    public void TestSmokeScenario()
    {
        // Initialize the application or its components
        // Perform basic operations to verify critical functionality
        // Assert that critical functionality is working as expected
    }
}

					

End-to-end testing

End-to-end testing replicates a user behavior with the software in a complete application environment. It verifies that various user flows work as expected and can be as simple as loading a web page or logging in or much more complex scenarios verifying email notifications, online payments, etc...

End-to-end tests are very useful, but they're expensive to perform and can be hard to maintain when they're automated. It is recommended to have a few key end-to-end tests and rely more on lower level types of testing (unit and integration tests) to be able to quickly identify breaking changes.

Example
						using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

[TestClass]
public class EndToEndTests
{
    private IWebDriver driver;

    [TestInitialize]
    public void Initialize()
    {
        // Set up the WebDriver (e.g., ChromeDriver)
        driver = new ChromeDriver();
    }

    [TestMethod]
    public void TestEndToEndScenario()
    {
        // Navigate to the application's URL
        driver.Navigate().GoToUrl("https://example.com");

        // Perform actions and assertions to test the end-to-end scenario

        // Example:
        // IWebElement element = driver.FindElement(By.Id("someElementId"));
        // element.Click();
        // Assert.AreEqual("Expected Result", driver.Title);
    }

    [TestCleanup]
    public void Cleanup()
    {
        // Clean up resources, close the browser, etc.
        driver.Quit();
    }
}

					

Summary

In this article I presented just a few types of software testing in C#. Depending on your project's requirements, you may also encounter other types like security testing, usability testing, and more. The choice of testing type depends on what aspects of your software you need to validate and ensure its quality.