1

I am new to c# unit testing. I am curious if it is possible to parameterize the SetUp method that is decorated by [TestInitialize]

    [TestInitialize]
    public void SetUp(string path)
    {
        fileStream = new FileStream(path, FileMode.Open);
        binaryReader = new BinaryReader(fileStream);
    }

For example, before each test, I want to be able to pass a unique path to SetUp. Can this be done? I have consulted this post: How to run a test method with multiple parameters in MSTest? but it does not quite address the problem I am trying to solve, and if it is even possible. Thanks to all of those who respond in advance!

datainc
  • 45
  • 6
  • No, it isn't possible. However, you can invoke a common method from multiple tests. That would enable similar behavior where the path is declared per test. – David L Jun 29 '20 at 21:10
  • Mind posting an example? – datainc Jun 29 '20 at 21:12
  • "For example, before each test, I want to be able to pass a unique path to SetUp. " - and where would you parameterize that given taht you start A test - and the setup routine runs in the backround before it WITHOUT UI? – TomTom Jun 30 '20 at 14:12

1 Answers1

1
public void SetUp(string path)
{
    fileStream = new FileStream(path, FileMode.Open);
    binaryReader = new BinaryReader(fileStream);
}

This is how I ended up doing it... removed the decorator and called SetUp() explicitly in each test.

datainc
  • 45
  • 6