TDD
Testing (XUNIT)
[Fact]
[Fact] is used for a single test. The function can not contain data parameters
example
[Fact]
public void ReturnInitialisedCoordinates()
{
var move = new Move(2, 2);
Assert.Equal(2, move.X);
Assert.Equal(2, move.Y);
}
[Theory]
[Theory] is used to pass through multiple data tests into a single test function
[InLineData(parameters)] is used to pass the data into the test function
example
[Theory]
[InlineData("1,1")]
[InlineData("3,2")]
[InlineData("q")]
[InlineData("2,2")]
public void ValidateCorrectInput(string input)
{
var inputHandler = new UserInputHandler();
bool valid = inputHandler.IsValid(input);
Assert.True(valid);
}
Another example which includes passing through a list
[Theory]
[InlineData(new object[] {new string[] {"1", "11"}})]
[InlineData(new object[] {new string[] {"11", "1"}})]
[InlineData(new object[] {new string[] {"12345", "09766", "12"}})]
public void DetrermineFalseConsistency3(string[] numbers)
{
var checker = new Checker();
var result = checker.CheckConsistency(numbers.ToList());
Assert.False(result);
}
Asserts
The asserts which I use regularly are:
- Assert.Equal (expected, result)
- Assert.True (boolean data)
- Assert.False (boolean data)
Class Structure
At myob we create a new test class for each test we use in the actual code.
for example, if I was going to make a Player class, I would first make a test class called PlayerShould and write all the unit tests for the Player class. Each test in the test class would be named a feature that the player class should have.
Written on April 8, 2018