wasAsked.

steps to do xunit testing in c#

EY · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

Create a test project, reference the production project, arrange inputs and dependencies, execute one behavior, and assert the observable result. Keep unit tests deterministic and fast; use integration tests when the behavior depends on a real database, filesystem, queue, or HTTP boundary. ```bash dotnet new xunit -n Shop.UnitTests dotnet add Shop.UnitTests reference Shop.Application dotnet test ``` ```csharp public sealed class PriceCalculatorTests { [Theory] [InlineData(100, 0.10, 90)] [InlineData(50, 0, 50)] public void ApplyDiscount_ReturnsExpectedTotal( decimal subtotal, decimal rate, decimal expected) { var sut = new PriceCalculator(); decimal actual = sut.ApplyDiscount(subtotal, rate); Assert.Equal(expected, actual); } [Fact] public void ApplyDiscount_RejectsNegativeSubtotal() { var sut = new PriceCalculator(); Assert.Throws<ArgumentOutOfRangeException>( () => sut.ApplyDiscount(-1, 0.1m)); } } ``` Name tests around behavior, cover boundaries and failure paths, and avoid asserting private implementation details. CI should run `dotnet test` from a clean checkout.

Krishna Yadav · · 0 likes

Sign in to add your answer

Related interview topics