3

I have inherited a test project with thousands of test methods / classes and I need to implement a new clean up procedure that needs to run after or before every test. I know this is not the best but this is the current situation I am in. In a year from now we will hopefully have all these tests rebuilt properly. I need to some how run a method before or after every test without hard codding the method in every single test class as we just do not have time for this. I know there are attributes for assembly clean up and init, but that only runs before and after the entire assembly. I need something like this, but that runs after every test without coding that functionality per test.

Is this possible? What are some options?

EDIT I am using MSTest

2
  • What testing framework are you using? Also, no need for thanks in your question.
    – xDaevax
    Aug 11, 2014 at 20:36
  • Added to description Aug 12, 2014 at 13:53

2 Answers 2

7

Assuming you're using MSTest, the attribute you need for the set up method is [TestInitialize]. A method decorated with this attribute will run before every single test.

5
  • 4
    And use [TestCleanup] for the cleanup part.
    – ChriPf
    Aug 12, 2014 at 14:02
  • 2
    That only works if it is in EVERY single test class, so this will not work for me. I need a single method hook. Aug 12, 2014 at 14:02
  • 2
    @Landin Martens: create a BaseTest class and inherits all your test from it. Then you would add the initialize method there and will apply to all per inheritance. This is the best you can do. Aug 12, 2014 at 14:03
  • There are way to many tests to do this, that Is why I was hoping for some way to hook into some sort of event as I do not have time for that. Aug 12, 2014 at 20:08
  • @Landin Martens: you could do a massive replace using visual studio replace tool. It's not hard at all to do and is the best you can do, there is no magic. Aug 13, 2014 at 14:51
0

Sorry for the necro-answer, but I came across this situation myself, and figured I should add to the knowledge-base.

First up, the answer provided above is absolutely correct in the one half: you want a function with [TestInitialize], and it has to be in the same class as the [TestMethod].

Even doing some shenanigans like wrapping all your test files in a 'public partial class GlobalTesting { ... }' doesn't work, because the TestInitialize doesn't trickle down into subclasses.

So... you're going to need to have every test class either have a TestInitialize method, or you're going to need to have them derive off one that does.

"But we've got thousands of test classes!"

Understood. In my case, it was only a few classes, so I could do it manually. But if you couldn't?

Regex for the win!

Regex gets my vote for the most underused technology in programming. In this case, here's my semi-crude attempt at a regex find-replace in Notepad++

Search String: (\[TestClass\]\s*\w*\s*\w*\s*class\s+[!-~]*)(\s*\{)
Replace String: \1 : ParentClassToPerformCleanups \2

What does this do? It searches for:

Capture Group #1
    [TestClass]
    any amount of white space
    optionally a single word with optional whitespace behind it
    optionally another single word with optional whitespace behind it
    the word 'class'
    at least one whitespace character
    a function name
Capture Group #2
    any amount of white space
    a { brace

... aka, any classes which are marked [TestClass], but don't derive from anything.

Don't get me wrong - this is pretty simplistic. You might have additional quirks, like multiple attributes, or classes that implement interfaces or other complications. In which case, you'll have to make the regex a bit more robust. But... with something like this, it'll make altering the class definitions of thousands of classes much easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.