Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Method to be called before any test (xUnit)

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:

  1. Call Prepare()
  2. Call Test1
  3. Call Prepare()
  4. Call Test2

I know that i can call it by my own like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading