Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you've already mentioned the Ordered Test functionality that the Visual Studio testing framework supplies, I'll ignore that. You also seem to be aware that what you're trying to accomplish in order to test this Static Class is a "bad idea", so I'll ignore that to.</p> <p>Instead, lets focus on how you might actually be able to guarantee that your tests are executed in the order you want. One option (as supplied by @gaog) is "one test method, many test functions", calling your test functions in the order that you want from within a single function marked with the <code>TestMethod</code> attribute. This is the simplest way, and the only disadvantage is that <strong>the first test function to fail will prevent any of the remaining test functions from executing</strong>. </p> <p>With your description of the situation, this is the solution I would suggest you use.</p> <p>If the bolded part is a problem for you, you can accomplish an ordered execution of isolated tests by leveraging the in built data driven test functionality. Its more complicated and feels a bit dirty, but it gets the job done.</p> <p>In short, you define a data source (like a CSV file, or a database table) that controls the order in which you need to run your tests, and names of the functions that actually contain the test functionality. You then hook that data source into a data driven test, use the sequential read option, and execute your functions, in the order you want, as individual tests.</p> <pre><code>[TestClass] public class OrderedTests { public TestContext TestContext { get; set; } private const string _OrderedTestFilename = "TestList.csv"; [TestMethod] [DeploymentItem(_OrderedTestFilename)] [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", _OrderedTestFilename, _OrderedTestFilename, DataAccessMethod.Sequential)] public void OrderedTests() { var methodName = (string)TestContext.DataRow[0]; var method = GetType().GetMethod(methodName); method.Invoke(this, new object[] { }); } public void Method_01() { Assert.IsTrue(true); } public void Method_02() { Assert.IsTrue(false); } public void Method_03() { Assert.IsTrue(true); } } </code></pre> <p>In my example, I have a supporting file called TestList.csv, which gets copied to output. It looks like this:</p> <pre><code>TestName Method_01 Method_02 Method_03 </code></pre> <p>Your tests will be executed in the order that you specified, and in normal test isolation (i.e. if one fails, the rest still get executed, but sharing static classes). </p> <p>The above is really only the basic idea, if I were to use it in production I would generate the test function names and their order dynamically before the test is run. Perhaps by leveraging PriorityAttribute you found and some simple reflection code to extract the test methods in the class and order them appropriately, then write that order to the data source.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload