Add method invocation parameters to BeforeAfterTestAttribute API to better support Theory tests
In the use case where using Theory annotated methods, where the arguments should follow a IDisposable pattern for example
public interface IAutoInit
{
public void Init();
}
public class Foo : IDisposable, IAutoInit
{
public Init() { ... init some global state ... }
public void Dispose() { .. clean up state ... }
}
public class TestFixture
{
[Theory, MemberData("SomeFooSource")]
public void Test(Foo foo)
{
using(foo) { // boiler plate
foo.Init(); // boiler plate
... perform test...
}
}
}
Ideally would like to be able to avoid the boiler plate using(foo) { foo.Init() by adding support for the actual test method arguments in the BeforeAfterTestAttribute.Before/After API. Looking at the code for the test invoker, seems to be trivial to pass along the actual method arguments?
class AutoInitAttribute : BeforeAfterTestAttribute
{
public override void Before(MethodInfo methodUnderTest, object[] args)
{
(args[0] as IAutoInit).Init()
}
public override void After(MethodInfo methodUnderTest, object[] args)
{
(args[0] as IDisposable).Dispose()
}
}
public class TestFixture
{
[Theory, MemberData("SomeFooSource")]
public void Test(Foo foo)
{
... perform test...
}
}
This violates the layering intention that we had for BeforeAfter attributes; namely, that they were intended to be highly reusable orthogonal components (for example, things which set and then rollback environmental settings like thread culture).
How would you specify which thread culture to use, based on other attributes on the method (i.e. method info metadata)? In general, it seems a bit odd to not include this information given that you support data driven tests via Theory attribute. Feels like you a making a pretty strong assumption of what the developer might or might not be able to do, as opposed to deferring the decision to the developer, and making the feature a bit more versatile.
We'd need access to the TestFixture, as we want some specific cleanup/reset of parts of the fixture before and after each test. Is there any way to access the fixture?
We'd need access to the TestFixture, as we want some specific cleanup/reset of parts of the fixture before and after each test. Is there any way to access the fixture?
Not at this time. Please open a new feature request for this.
Closed for age.