diff --git a/404.html b/404.html index dcb267ce0d..7b98b00170 100644 --- a/404.html +++ b/404.html @@ -4,7 +4,7 @@
Type
of the class containing your test source data method, and the second should be the name of the method.If methods are returning reference types, they should return a Func<T>
rather than just a T
- This ensures each test has its own instance of that object and tests aren't sharing objects which could lead to unintended side effects.
Here's an example returning a simple object:
-using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public record AdditionTestData(int Value1, int Value2, int ExpectedResult);
public static class MyTestDataSources
{
public static AdditionTestData AdditionTestData()
{
return new AdditionTestData(1, 2, 3);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(AdditionTestData additionTestData)
{
var result = Add(additionTestData.Value1, additionTestData.Value2);
await Assert.That(result).IsEqualTo(additionTestData.ExpectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public record AdditionTestData(int Value1, int Value2, int ExpectedResult);
public static class MyTestDataSources
{
public static Func<AdditionTestData> AdditionTestData()
{
return () => new AdditionTestData(1, 2, 3);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(AdditionTestData additionTestData)
{
var result = Add(additionTestData.Value1, additionTestData.Value2);
await Assert.That(result).IsEqualTo(additionTestData.ExpectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
This can also accept tuples if you don't want to create lots of new types within your test assembly:
-using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public static class MyTestDataSources
{
public static (int, int, int) AdditionTestData()
{
return (1, 2, 3);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(int value1, int value2, int expectedResult)
{
var result = Add(value1, value2);
await Assert.That(result).IsEqualTo(expectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public static class MyTestDataSources
{
public static Func<(int, int, int)> AdditionTestData()
{
return () => (1, 2, 3);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(int value1, int value2, int expectedResult)
{
var result = Add(value1, value2);
await Assert.That(result).IsEqualTo(expectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
This attribute can also accept IEnumerable<>
. For each item returned, a new test will be created with that item passed in to the parameters.
Here's an example where the test would be invoked 3 times:
-using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public record AdditionTestData(int Value1, int Value2, int ExpectedResult);
public static class MyTestDataSources
{
public static IEnumerable<AdditionTestData> AdditionTestData()
{
yield return new AdditionTestData(1, 2, 3);
yield return new AdditionTestData(2, 2, 4);
yield return new AdditionTestData(5, 5, 10);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(AdditionTestData additionTestData)
{
var result = Add(additionTestData.Value1, additionTestData.Value2);
await Assert.That(result).IsEqualTo(additionTestData.ExpectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public record AdditionTestData(int Value1, int Value2, int ExpectedResult);
public static class MyTestDataSources
{
public static IEnumerable<Func<AdditionTestData>> AdditionTestData()
{
yield return () => new AdditionTestData(1, 2, 3);
yield return () => new AdditionTestData(2, 2, 4);
yield return () => new AdditionTestData(5, 5, 10);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(AdditionTestData additionTestData)
{
var result = Add(additionTestData.Value1, additionTestData.Value2);
await Assert.That(result).IsEqualTo(additionTestData.ExpectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
This can also accept tuples if you don't want to create lots of new types within your test assembly:
-using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public static class MyTestDataSources
{
public static IEnumerable<(int, int, int)> AdditionTestData()
{
yield return (1, 2, 3);
yield return (2, 2, 4);
yield return (5, 5, 10);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(int value1, int value2, int expectedResult)
{
var result = Add(value1, value2);
await Assert.That(result).IsEqualTo(expectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;
namespace MyTestProject;
public static class MyTestDataSources
{
public static IEnumerable<Func<(int, int, int)>> AdditionTestData()
{
yield return () => (1, 2, 3);
yield return () => (2, 2, 4);
yield return () => (5, 5, 10);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(int value1, int value2, int expectedResult)
{
var result = Add(value1, value2);
await Assert.That(result).IsEqualTo(expectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}