Does TUnit support constructor parameters? #589
-
We are using NUnit with heavy use of constructor parameters - this way we can easily run all tests against all supported database types. Is this supported also by TUnit? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 6 replies
-
Yes, see here: https://thomhurst.github.io/TUnit/docs/tutorial-basics/data-source-driven-tests |
Beta Was this translation helpful? Give feedback.
-
That's not what I need, it's not only about the lifetime of a data source, it's about data source on class level that automatically applies to all tests. public enum TestDbEngine
{
Mongo,
EFCorePostgres
}
[TestFixture(TestDbEngine.Mongo)]
[TestFixture(TestDbEngine.EFCorePostgres)]
public class TestClass(TestDbEngine dbEngine)
{
[OneTimeSetUp]
public override async Task OneTimeSetUp()
{
_dbContainer = dbEngine switch
{
TestDbEngine.EFCorePostgres => new PostgreSqlBuilder()
.WithImage(TestImages.PostgreSql)
.Build(),
TestDbEngine.Mongo => new MongoDbBuilder()
.WithImage(TestImages.MongoDb)
.Build()
};
await _dbContainer.StartAsync();
}
[TestCase(0)]
[TestCase(1)]
public void Test(int testCase)
{
// Some test
}
... and many more tests.... This will run a matrix of tests with all their test cases against all databases |
Beta Was this translation helpful? Give feedback.
-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public sealed class ArgumentsAttribute : TUnitAttribute
{ Looking at the attribute targets, it should be possible, right? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
If there is no movement in the test API, that would be better than nothing (maybe configurable by attribute?) |
Beta Was this translation helpful? Give feedback.
-
@thomhurst btw. any idea how to show the test output (Console or TestContext.Current.OutputWriter) in VS Test explorer? |
Beta Was this translation helpful? Give feedback.
@tom-englert This should work in v0.1.818 now if you want to give it a try