Is there any way to create method that will be automatically called before any test?
[Fact]
public void Test1() { }
[Fact]
public void Test2() { }
private void Prepare()
{
// Prepare test environment
}
I need Prepare to be called before Test1 and Test2. It should be like this:
- Call
Prepare() - Call
Test1 - Call
Prepare() - Call
Test2
I know that i can call it by my own like
[Fact]
public void Test1()
{
Prepare();
}
but is there any way to do it automatically?
>Solution :
Include that call to the Prepare method in the constructor of your test class.
The documentation comparing other testing frameworks that have e.g. a [SetUp] attribute or alike mentions below.
We believe that use of
[SetUp]is generally bad. However, you can implement a parameterless constructor as a direct replacement.