2
votes

Here's my scenario - coding in C# using NUnit, I have these tests:

  • Test1
  • Test2
  • Test3
  • Test4
  • TestX

The first four of these tests all function independently, but I need TestX to act a little differently. This test will need to function differently depending on which of the previous tests I've just run.

For instance, if I choose to run Test1, Test2 and TestX all in one run then TestX will act in one way, but if I instead run Test3, Test4 and then TestX, it would act differently. I'm hoping there's a way of accessing which tests have been used in a particular run and feed that into a switch statement to change TestX accordingly.

Clarification: I don't need TestX to know the results of the first four test or anything about how they ran, just simply whether they have been run or not. Also, when I say refer to a single "test run", I mean CTRL clicking several tests in the Test Explorer and then running them all in one go.

I can't seem to access this information from the TestContext, but I'm really hoping that there's another way that I've yet to think of. Any help would be appreciated - thanks very much.

1

1 Answers

0
votes

First off, let me say that, IMHO, what you want to do is a Bad Idea.

Having said that, the only reliable way I can think off will to base the behaviour of TestX on some observable side effects of the other tests running. So if the tests all add data to a table in your DB then have TestX look which data is created then behave accordingly. You should not do this.

Now back to what I think you should do:

have several variations of TestX each of which tests what TestX should do if each of the preceding tests have been run, ie put each part of your proposed switch statement into a separate test, and have the Arrange (or setup) of that test do the same work as running the preceding test does.

This might seem like more work, but it will give you better more repeatable tests which I think will be more maintainable.