@manhng

Welcome to my blog!

Fake & Unit Testing

September 2, 2020 10:35

Fake & Unit Testing (edit)

There are three Unit Test project types for .NET Core.

  • MSTest Test Project (.NET Core)
  • xUnit Test Project (.NET Core)
  • NUnit Test Project (.NET Core)

This article focus on:

  • Visual Studio 2019 Enterprise
    • Test Explorer
    • Live Unit Testing
  • ASP.NET Core 3.1
  • EF Core 3.1
  • xUnit
    • [Fact]
    • [Theory]
    • [InlineData]
    • [ClassData] 
    • [MemberData] 
    • [DataAttribute]
    • IEnumerable<object[]>
  • Moq
  • Faker.js
  • Integration tests
  • Selenium WebDriver

https://github.com/Ermesx/testing-context

https://github.com/bchavez/Bogus

Code template

https://wakeupandcode.com/unit-testing-in-asp-net-core/

public class Calculator
{
    public int Add(int value1, int value2)
    {
        return value1 + value2;
    }
}
public class CalculatorTests
{
    [Fact]
    public void CanAdd()
    {
        var calculator = new Calculator();

        int value1 = 1;
        int value2 = 2;

        var result = calculator.Add(value1, value2);

        Assert.Equal(3, result);
    }
}
[Theory]
[InlineData(1, 2, 3)]
[InlineData(-4, -6, -10)]
[InlineData(-2, 2, 0)]
[InlineData(int.MinValue, -1, int.MaxValue)]
public void CanAddTheory(int value1, int value2, int expected)
{
    var calculator = new Calculator();

    var result = calculator.Add(value1, value2);

    Assert.Equal(expected, result);
}
public void TestAddNumbers(int x, int y, int expectedResult)
{
   // 1. Arrange
   var cs = new CalcService();

   // 2. Act 
   var result = cs.AddNumbers(x, y);

   // 3. Assert 
   Assert.Equal(expectedResult, result);
}

References:

https://www.simongilbert.net/unit-testing-xunit-csharp-dotnetcore/

https://wakeupandcode.com/unit-testing-in-asp-net-core/

https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdata/

https://www.automatetheplanet.com/visual-studio-project-templates/

https://code-maze.com/unit-testing-aspnetcore-web-api/

https://www.syncfusion.com/blogs/post/how-to-integrate-unit-testing-with-asp-net-core-3-1.aspx

https://www.sqlshack.com/sql-unit-testing-with-ssdt-sql-server-data-tools/

Categories

Recent posts