0

So a TestMethod runs only once in one test run.

How can I, in a single test run, let a TestMethod run several times, each time for a different data set that I've set up? My data does not come from a database or file; I want to build up several different in-memory instances of test data mockup.

TestInitialize doesn't let me do this as it runs only once, as well.

What's in control of the execution of TestMethods? How to make it re-run my TestMethods for each data set and how do I access the data set then?

I thought TestContext would be useful but it seems to be database only?

2 Answers 2

1

What you're looking for is so-called Data-driven testing. Look e.g. here and here for descriptions on how to do it with MSTest.

HTH.
Thomas

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This is basically what I was looking for.
The second link is broken. Could you look to see if you can fix it please?
0

You could define a test method which calls the other test method multiple times, after doing the correct setup, I'm not saying that this is a good thing to do, but I believe it would work

public class TestClass
{
//This is where the per-data-source test is. This is not marked as a TestMehod because
//it will not be invoked directly by the test runner.
public void ActualTest()
{
//Per-data-source test logic here.
}
[TestMethod]
public void RunActualTestsMultipleTimesWithDifferentConfigs()
{

//Setup for test run with data set 1
ActualTest();
//Setup for test with data set 2
ActualTest();
}
}

This feels like a terrible, terrible hack, I freely admit that. I wouldn't use this myself if I had any other choice, but it may be an option.

Another possibility is to look into how extensible MSTest is, specifically whether or not there is any mechinism to modify or extend the test runner

1 Comment

Extending the test framework would be a whole different project I think but it's interesting. MSDN doesn't give descriptions of methods there and I didn't see any 'virtuals'. Anyway this stuff can be found in the namespace Microsoft.VisualStudio.TestTools.Framework.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.