NUnit is a unit-testing framework for all .Net languages. Initially ported from JUnit, the current production release, version 3, has been completely rewritten with many new features and support for a wide range of .NET platforms.
NuGet Packages
NUnit
NUnit3TestAdapter
Microsoft.NET.Test.Sdk
Your First NUnit Test Case
Add [TestFixture] and [Test] to mark code as tests
Test can be run in Test Explorer and in Command Line
Why Write Automated Tests?
Help to find defects and regressions. When we make a change to the project, we may find that unintentionally break one of the existing tests. Something that once working is no longer working.
Automated Tests give us greater confidence that the software is working as it should.
Understanding the NUnit Test Framework
NUnit Library
Attributes e.g. [Test]
Assertions
Test Runner
Recognizes attributes
Execute test methods
Report test results
Test explorer
donet test
NUnit attributes Overview
[TestFixture]: Mark a class that contains tests
[Test]: Mark a method as a test
[Category]: Organize tests into categories
[TestCase]: Data driven test cases
[Values]: Data driven test parameters
[Sequential]: How to combine test data
[SetUp]: Run code before each test
[OneTimeSetUp]: Run code before first test in class
NUnite Assertions Overview
1 2 3
// Constraint Model of assertions (newer) Assert.That(sut.Years, Is.EqualTo(1)); Assert.That(test result, constraint instance);
This Classic Model is still supported but since no new features have been added to it for some time. the constraint-based model must be used in order to have full access to NUnit’s capabilities.
1 2 3 4
Classic Model of assertions (older) Assert.AreEqual(1, sut.Years); Assert.NotNull(sut.Years); Assert.xyz(...);
The Logical Arrange, Act, Assert Test Phases
Arrange: Set up test objects, initialize test data
Act: call methods, set property, to cause some effect in the project
Assert: compare returned value/end state with expected
Qualities of Good Tests
Fast
Repeatable
Isolated: One Test should not depend on others to run
Trustworthy
Valuable
Asserting on Different Types of Results
Asserts: Evaluate and verify the outcome of a test based on a returned result, final object state, or the occurence of events observed during execution. An assert should either pass or fail.
How many asserts per test?
A single test usually focuses on testing a single ‘behaviour’. Multiple asserts are usually ok if all the asserts are related to testing this single behaviour.
Asserting on Equality
1 2 3 4 5 6 7
// compare value Assert.That(a, Is.EqualTo(...)); Assert.That(a, Is.Not.EqualTo(...));
[Test] [TestCaseSource(typeof(MonthlyRepaymentTestData), "TestCases")] public void CalculateCorrectMonthlyRepayment_Centralized(decimal principal, decimal interestRate, int termInYears, decimal expectedMonthlyPayment) { var sut = new LoanRepaymentCalculator();
var monthlyPayment = sut.CalculateMonthlyRepayment( new LoanAmount("USD", principal), interestRate, new LoanTerm(termInYears));
[Test] [TestCaseSource(typeof(MonthlyRepaymentCsvData), "GetTestCases", new object[] { "Data.csv" })] public void CalculateCorrectMonthlyRepayment_Csv(decimal principal, decimal interestRate, int termInYears, decimal expectedMonthlyPayment) { var sut = new LoanRepaymentCalculator();
var monthlyPayment = sut.CalculateMonthlyRepayment( new LoanAmount("USD", principal), interestRate, new LoanTerm(termInYears));