1

I am using Assembly Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0

I have a [TestClass] which includes only tests concerned with publishing and subscribing to messages. However it's possible that the message broker client that we use is not available on the current environment or for some other reason, which is controller with a falg in the appsettings:

var messageBrokerConfig = configuration.GetSection("MessageBroker").Get<BrokerConfig>();
if (messageBrokerConfig.Enabled)...

this is the way I know if the message broker is available and I can execute the tests or not. Now, a simple solution, that immediatly comes to mind is just to have some (maybe private method) which would be called at the beginning of each test like:

    private bool ShouldExecute()
    {
        return configuration.GetSection("MessageBroker").GetSection("Enabled") == false.ToString()
    }

But then I should put this at the beginning of every test which is pretty far from DRY.

In a perfect scenarion I would be able to able/disable the [TestClass] attribute or have any other attribute or something that will prevent all test from executing. At worst, maybe when I invoke the [ClassInitialize] method. But I don't know the framework very well so I'm not sure what are my options here.

I'm pretty sure there should be a solution which will allows me to put a code at one place and decide for the whole class but I just don't know what it is.

Leron
  • 9,546
  • 35
  • 156
  • 257
  • 1
    Cool username! I believe what you're looking for is well described in the following article, under *Execution Extensions*: https://devblogs.microsoft.com/devops/extending-mstest-v2/ – CoolBots Aug 10 '20 at 20:49
  • Glad to read you like the handle :) Regarding the questions, this is a very good approach, I only wonder, in my custom attribute how I would terminate the execution exactly? When I override `public override TestResult[] Execute(ITestMethod testMethod) ` what should I exactly include, just ` return false` or somethng else...? – Leron Aug 10 '20 at 21:03
  • I actually found a StackOverflow answer using this technique. It appears they're returning `new TestResult() { Outcome = UnitTestOutcome.Inconclusive }`: https://stackoverflow.com/questions/53338872/how-to-skip-a-unit-test-at-runtime – CoolBots Aug 10 '20 at 21:25

0 Answers0