Turn your manual testers into automation experts Request a DemoStart testRigor Free

ASP.Net Testing

ASP.Net Testing

ASP.NET is part of Microsoft’s .NET ecosystem that offers a host of capabilities for building applications and services. ASP stands for ‘active server page’, which deals with the backend scripting of web pages. ASP.NET is an open-source framework that is widely used to build web applications. It offers frameworks such as Web Forms, ASP.NET MVC, and ASP.NET Web Pages for developing web pages. Depending on your use case, you can use three of these frameworks in combination. Although ASP.NET was intended for server-side scripting for Windows-based applications, Microsoft has extended this to other platforms with their ASP.NET Core framework. You can read more about the differences between these frameworks here.

When it comes to testing ASP.NET framework-based applications, these are the primary testing types:

Unit Testing

Unit testing forms the basis of the testing activities where you validate if every unit of code is functioning as expected. These tests are usually faster and less expensive in comparison to integration and end-to-end testing methods. The ASP.NET framework offers support to the following tools to perform unit testing.

With the .NET CLI or other IDEs, you can easily execute your unit tests. Additionally, Visual Studio, which is another product by Microsoft, is a great IDE to write test cases in as it seamlessly supports .NET-related tools and frameworks.

For writing your test cases, you can keep in mind the arrange, act, and assert flows. With this approach, you first need to arrange your dependencies for the test, act on them by writing the test, and assert the outcome. In the .NET Core framework, irrespective of whether the pages are designed using the Razor pages framework or MVC, unit tests should be written.

Below is a sample of a unit test:

[Fact]
public async Task Post_DeleteAllMessagesHandler_ReturnsRedirectToRoot()
{
  // Arrange
  var defaultPage = await _client.GetAsync("/");
  var content = await HtmlHelpers.GetDocumentAsync(defaultPage);

  //Act
  var response = await _client.SendAsync(
      (IHtmlFormElement)content.QuerySelector("form[id='messages']"),
      (IHtmlButtonElement)content.QuerySelector("button[id='deleteAllBtn']"));

  // Assert
  Assert.Equal(HttpStatusCode.OK, defaultPage.StatusCode);
  Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
  Assert.Equal("/", response.Headers.Location.OriginalString);
}

Integration Testing

Integration tests are meant to verify if two or more components are working together properly. This is generally a wide term, covering different layers of testing activities. Quite often, the test cases target those scenarios where integrations with databases, file systems, network infrastructure, or other components are expected. Unlike unit tests, where you fake or mock input parameters, integration tests utilize real data.

Some ways of writing integration tests are:
  • Checking crucial read-write operations into databases.
  • Services can be tested by mocking HTTP calls.
  • Validating GET and POST routes, controllers, and pipelines.

You can utilize the TestServer class available, which helps mimic HTTP calls for facilitating integration tests. It makes the execution of your tests faster.

The example below is of an integration test case to check if the correct players are being displayed.

public class PlayersControllerIntegrationTests : IClassFixture>
{
  private readonly HttpClient _client;
  
  public PlayersControllerIntegrationTests(CustomWebApplicationFactory factory)
  {
    _client = factory.CreateClient();
  }
  
  [Fact]
  public async Task CanGetPlayers()
  {
    // The endpoint or route of the controller action.
    var httpResponse = await _client.GetAsync("/api/players");
  
    // Must be successful.
    httpResponse.EnsureSuccessStatusCode();
  
    // Deserialize and examine results.
    var stringResponse = await httpResponse.Content.ReadAsStringAsync();
    var players = JsonConvert.DeserializeObject>(stringResponse);
    Assert.Contains(players, p => p.FirstName=="Jake");
    Assert.Contains(players, p => p.FirstName == "Erica");
  }
}

Another example that validates whether response headers are appropriate after sending a URL request:

public class BasicTests 
    : IClassFixture<WebApplicationFactory<RazorPagesProject.Startup>>
{
  private readonly WebApplicationFactory<RazorPagesProject.Startup> _factory;

  public BasicTests(WebApplicationFactory<RazorPagesProject.Startup> factory)
  {
      _factory = factory;
  }

  [Theory]
  [InlineData("/")]
  [InlineData("/Index")]
  [InlineData("/About")]
  [InlineData("/Privacy")]
  [InlineData("/Contact")]
  public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
  {
      // Arrange
      var client = _factory.CreateClient();

      // Act
      var response = await client.GetAsync(url);

      // Assert
      response.EnsureSuccessStatusCode(); // Status Code 200-299
      Assert.Equal("text/html; charset=utf-8", 
          response.Content.Headers.ContentType.ToString());
  }
}

End-to-End Testing

End-to-end testing involves verifying a complete workflow. The goal of such tests is to emulate entire user flows, making sure that all components behave well together. End-to-end tests are performed on the UI layer – and the closer your test can resemble real user behavior, the better. End-to-end tests are typically the slowest among the three types of testing discussed in this article; however, they are also the most accurate. While developers typically write unit tests, end-to-end tests are commonly written by the QA team.

You can build these types of tests with the following tools:
  • Selenium-based tools
  • testRigor

Selenium-based tools for end-to-end testing

Selenium is synonymous with testing and is widely used. There are many tools available that simplify adopting Selenium for testing. However, each of these tools does inherit some of the drawbacks that Selenium has, though they try to overcome most of them. There are hundreds of articles that you can easily find on this topic, so we won’t cover it here in greater detail.

testRigor for end-to-end testing

testRigor is a powerful, no-code automation solution that allows you and your team to express commands in plain English, thus making it very easy to create automated test cases.

  1. Its AI-powered engine can interpret the relative position of the element from the mentioned command. Thus any changes in the locator value of the element do not break the test. It also provides the capability to test by comparing images to find the element.
  2. testRigor supports cross-platform and cross-browser testing without major configuration steps.
  3. Provides support for typing and copy-pasting use cases.
  4. Has provision to support 2FA testing support for Gmail, text messages, and Google authenticator.
  5. Easy integration with most CI/CD tools.
  6. Facilitates easy mocking of API calls and accessing databases if needed.

The tool provides good reporting capabilities, which can help you review and understand issues in your test cases, if any.

Here’s a test example:

hover over "Account & Lists"
click "Sign in"
Check that the page contains "Sign in"
Enter stored value "mobilePhone" in "Email and mobile phone number"
Enter stored value "password" in "Password"
Click on "Sign in"
Select "office products" from "category"
Enter "table organizer" in "Search"
Click on "search" to the left of "EN"
Select "HermanMiller" from "Brands"
Click on 1st "HermanMiller Office Chair"
Click on "Add gift options"
Click on "Add to cart"

Conclusion

The .NET ecosystem comprises multiple frameworks, such as ASP.NET and ASP.NET Core. All these frameworks can be tested at various stages using strategies and tools described above based on your needs. We recommend having tests on each layer, which will lead to an optimal and robust testing strategy.