Modern Testing for .NET

  • master
  • dev
    using Shouldly;

    public class CalculatorTests
    {
        public void ShouldAdd()
        {
            var calculator = new Calculator();
            calculator.Add(2, 3).ShouldBe(5);
        }
    
        public void ShouldSubtract()
        {
            var calculator = new Calculator();
            calculator.Subtract(5, 3).ShouldBe(2);
        }
    }
    

    using Fixie;

    public class TestingConvention : Discovery
    {
        public TestingConvention()
        {
            Methods
                .Where(x => x.Name != "SetUp");
        }
    }
    

    using Fixie;

    public class TestingConvention : Discovery, Execution
    {
        public TestingConvention()
        {
            Methods
                .Where(x => x.Name != "SetUp");
        }
    
        public void Execute(TestClass testClass)
        {
            testClass.RunCases(@case =>
            {
                var instance = testClass.Construct();
    
                SetUp(instance);
    
                @case.Execute(instance);
            });
        }
    
        static void SetUp(object instance)
        {
            instance.GetType().GetMethod("SetUp")?.Execute(instance);
        }
    }
    

    public class CustomerSearcherTests
    {
        public async Task SetUp()
        {
            await SetUpSampleCustomersAsync(/* ... */);
        }

        public void ShouldGetCustomersByName()
        {
            var searcher = new CustomerSearcher();
            searcher.SearchByName("Smith").Select(x => x.FullName).ShouldBe(new[] { "Sally Johnson", "John Smith" });
        }

        public void ShouldGetCustomersByAddress()
        {
            var searcher = new CustomerSearcher();
            searcher.SearchByAddress("Elm Street").Select(x => x.FullName).ShouldBe(new[] { "Sally Johnson" });
        }
    }