Search code examples
mstestdata-driven-tests

How to explicitly exit Data driven test method in MSTest


I use Data driven testing feature in MS test framework. [DataSource] attribute specifies table. C# Method is marked as [TestMethod]. It works but sometimes I need to stop execution. For example, table has 100 rows. How to explicitly (prematurely) exit test method (stop the rest of DDT invocations) after some condition, say during invocation of this method for 50-th row?

 [DataSource("System.Data.SQLite", @"Data Source=D:\Test.db;", "TestTableName", 
     DataAccessMethod.Sequential)]
     [TestMethod]
    public void DataTest()
    {
        string userId = Convert.ToString(TestContext.DataRow["userid"]);
        string telephone = Convert.ToString(TestContext.DataRow["telephone"]);
        string email = Convert.ToString(TestContext.DataRow["email"]);

        // .....

        functionThatPerformsAssert(userId, telephone, email);
        // .....

    }

Solution

  • I solved this invoking Assert.Inconclusive from functionThatPerformsAssert() where _skipTest determines whether to skip current row in data driven test and to begin next row:

    if (_skipTest)
                Assert.Inconclusive("Test Skipped");