TDD
Resources
- 2010, Freeman & Pryce, Growing Object-Oriented Software, Guided by Tests
- 2002, Test Driven Development By Example, Kent Beck, Three Rivers Institute
- mocking with moq
Properties of a good unit test
properties of a | good unit test |
---|---|
atomic | should target a small piece of functionality, hard to maintain otherwise |
deterministic | pass or fail, never inconclusive |
repeatable | consistent with dependent objects changing, time passing and between runs |
order independent & isolated | should not run be forced to run in a specific order, other tests or dependencies should not prevent test passing |
fast | order of ms |
easy to setup | should not be cumbersome |
Pages
- NUnit
- xunit
- FluentAssertions docs
- Integration testing in ASP.NET Core with
Microsoft.AspNetCore.TestHost
- EF in memory providers
- DbSetExtensionMethods
- InMemoryProviders
- EFCore in memory provider. The InMemory provider is missing from EF though.
-
How to mock a db context / set?
var mediatorHandler = Substitute.For<IMediatorHandler>(); MyContext context = Substitute.For<MyContext>(mediatorHandler); var stubbedEntities = new List<TEntityIAmInterestedIn> { /* do populate here */ }; var set = Substitute.For< DbSet<TEntityIAmInterestedIn>, IQueryable<TEntityIAmInterestedIn>, IDbAsyncEnumerable<TEntityIAmInterestedIn>>() .Initialize(stubbedEntities.AsQueryable()); context.Set<TEntityIAmInterestedIn>().Returns(set); // further on, you might test your EF repository queries using your custom configured data stubs.
dotnet
- coverage
- VS 2017 free coverage
- vs 2017 integrated coverage
-
OpenCover
# run tests for framework and platform dotnet test --no-build -v diag -o ./bin/x86/Release/ dotnet test --framework netcoreapp1.1 --runtime win10-x86 # new test project with enable pack dotnet new xunit -p -lang c# # list available project types dotnet new -all # An open source code coverage tool (branch and sequence point) for all .NET Frameworks 2 and above (including Silverlight). # Also capable of handling 32 and 64 bit processes. Use ReportGenerator 1.9 for best viewing results (also available via Nuget). dotnet add package OpenCover --version 4.6.519
- Integration Testing for ASP.NET Core Applications Feb18