1

I've a scenario where I'm adding a common code which every Test Case needs to invoke before executing the [TestMethod] (Similar to a [TestInitialize]/[AssemblyInitialize]).

However there are 3125 Tests and I'll need to add this [TestInitialize] along with some [DeploymentItem] attribute on all these Test Classes.

I could think of these ways

  1. Manually change every Test Class to include TestInitialize (which is not efficient)
  2. Have an Abstract Base class which will have TestInitialize and let every Test Case extend from this Base Class. But This again needs change in all 3125 Test Classes to extend them from the Base Class.
  3. I could use [AssemblyInitialize] and use it. These still need change in around 82 assemblies. Some assemblies already have [AssemblyInitialize] so it will need a deeper look while pasting the code.

I was wondering - if there was a way in c#, where we can enforce every TestCase to invoke a certain method before running the tests without having to touch all these 3125 files?

Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
  • 1
    Reflection can be of use, BUT I recommend you to go with manual approach. On 3 thousands tests there is high probability of this method to vary from test to test. Especially if this method is setting some Environment, which should be set (such as DataBase scripts, drivers, etc) – eocron Dec 08 '18 at 07:43
  • Are you finding this one : https://stackoverflow.com/a/2382603/2133965 ? – VietDD Dec 08 '18 at 07:45
  • @VietDD I think, OP asked about per-test runs, not per-assembly runs. Different lifetimes. – eocron Dec 08 '18 at 07:47
  • So how about [TestInitialize] ? (https://stackoverflow.com/a/35064699/2133965) – VietDD Dec 08 '18 at 07:50
  • @VietDD I'll have to declare [TestInitialize] with in every [TestClass] that means all 3125 Test Classes need to have this [TestInitialize]. Also, these Classes are across Assemblies. Even TestAssembly needs around 50+ assemblies. – Kishore Bandi Dec 08 '18 at 07:55
  • @eocron How do we use reflection to achieve this? For that reflection code to execute, shouldn't every test case again invoke it? This common code is safe to be called in all 3K+ tests as its very – Kishore Bandi Dec 08 '18 at 08:09
  • Here is how to add to property. I think it is not far from adding it to something else. https://stackoverflow.com/questions/14663763/how-to-add-an-attribute-to-a-property-at-runtime – eocron Dec 08 '18 at 09:52

0 Answers0