Testing

Testing

Terms to be addressed:

  • Test automation
  • Mobile App Testing
  • Infrastructure as code testing
  • API testing
  • Resilience testing
...see more

Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.

Assertions | Playwright .NET

...see more

Naming unit tests is an essential aspect of writing maintainable and readable code. Good test names should be descriptive, concise, and follow a consistent naming convention. Here are some best practices for naming unit tests:

  • Be Descriptive: Use descriptive names that convey the purpose of the test.
    Clearly indicate what behavior or functionality is being tested.
  • Follow a Naming Convention: Adopt a consistent naming convention for your tests. For example, you might use the MethodName_StateUnderTest_ExpectedBehavior format. Consistency makes it easier for developers to understand the structure of your tests.
  • Use CamelCase or Underscores: Choose a naming style (CamelCase or underscores) and stick to it. Consistency is key for readability.
  • Avoid Abbreviations: Minimize the use of abbreviations in test names. Clear and complete names enhance readability.
  • Include Arrange-Act-Assert (AAA) Steps: Include the Arrange-Act-Assert steps in the test name. This helps in quickly understanding the purpose of the test.
  • Focus on One Concern: A test should focus on a single logical concept or behavior. Avoid testing multiple things in a single test case.
  • Use Meaningful Words: Choose words that accurately describe the behavior being tested. This makes it easier to understand the purpose of the test.
  • Avoid Special Characters: Avoid using special characters, spaces, or punctuation in test names. This helps with consistency and prevents potential issues with test runners.
  • Keep it Short and Sweet: Aim for brevity without sacrificing clarity. A good test name should be concise and to the point.
  • Update Test Names When Code Changes: If the functionality being tested changes, update the corresponding test name to reflect the new behavior.
...see more

Adding a category to a test for filtering purposes. The source code sample demonstrates how to use the Trait attribute in C# with xUnit to categorize a test as an "IntegrationTest." This categorization helps in filtering and organizing tests based on their nature or purpose.

By using the Trait attribute with key-value pairs like "Category" and "IntegrationTests," tests can be grouped and executed selectively during testing workflows.

Sample code snippet:

[Fact, Trait("Category", "IntegrationTests")]
public async Task AcquireToken_GetToken_TokenNotNullOrEmpty()
{
   // test implementation
}
...see more

Unit testing throwing exceptions in asynchronous methods involves verifying that an async method correctly throws expected exceptions under specific conditions. This ensures robust error handling and code reliability in asynchronous programming.

Here's a sample C# code snippet using xUnit for unit testing async methods that throw exceptions:

public async Task TestAsyncMethod()
{
    // Arrange

    // Act
    async Task Act() => await _service.AsyncMethod();

    // Assert
    await Assert.ThrowsAsync<Exception>(() => Act());
}

For further understanding and examples, you can refer to discussions and tutorials on this topic:

These resources provide insights into handling exceptions in async code within the context of unit testing using popular frameworks like xUnit.

Add to Set
  • .NET
  • Agile
  • AI
  • ASP.NET Core
  • Azure
  • C#
  • Cloud Computing
  • CSS
  • EF Core
  • HTML
  • JavaScript
  • Microsoft Entra
  • PowerShell
  • Quotes
  • React
  • Security
  • Software Development
  • SQL References
  • Technologies
  • Testing
  • Visual Studio
  • Windows
 
Sets