5

I have a Test Method which is calling 2 Sub Test Methods. Both the sub Methods are Data Driven from an XML file. If I run each sub methods they run fine and successful. However, when I run Main Test Method (caller of both sub methods) it finds TestContext.DataConnection and TestContext.DataRow as null.

    private TestContext testContext;
    public TestContext TestContext
    {
        get { return testContext; }
        set { testContext = value; }
    }
    [TestMethod]
    public void SaveEmpty_Json_LocalStorage()
    {
        // Testing JSON Type format export and save
         SetWindowsUsers();
        // Add Network Information
        SetWifiInformation();

        // More logic and assertions here.
        // More logic and assertions here.
        // More logic and assertions here.
    }

    [TestMethod]
    [DeploymentItem("input.xml")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
               "input.xml",
               "User",
                DataAccessMethod.Sequential)]
    public void SetWindowsUsers()
    {
      Console.WriteLine(TestContext.DataRow["UserName"].ToString())
      // MORE LOGIC and Asserts  
    }

    [TestMethod]
    [DeploymentItem("input.xml")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
               "input.xml",
               "WifiList",
                DataAccessMethod.Sequential)]
    public void SetWifiInformation()
    {
      Console.WriteLine(TestContext.DataRow["SSID"].ToString())
      // MORE LOGIC and Asserts  
    }

If I Run All, 2 Methods pass and 1 fails. If I run individually, SaveData_Json_LocalStorage Does not pass, always gets TestContext.DataRow as null. Is it okay to call multiple methods inside. What is best way of writing chained test cases.

rocky
  • 157
  • 3
  • 13
  • I've never actually seen the `DeploymentItem` and `DataSource` attributes, but I'm pretty sure they are the source of your problem. Attributes don't actually do anything on their own. You need the unit test framework to do something with them. (Set up your data in this case.) When you call `SetWindowsUsers` and `SetWifiInformation` directly, that attribute-based setup is not performed. – Jason Watkins May 18 '15 at 20:26
  • 1
    In general you should avoid chaining test cases. It is up to the Test Runner to decide the order of execution. Instead use a general setup method for the test cases. – Henrik May 18 '15 at 20:29
  • @JasonWatkins When I call SetWindowsUsers and SetWifiInformation directly, Both of the attributes "DataSource" and DeploymentItems works fine and I get Data from XML and Test pass. I dont get my TestContext.DataRow as null. These two attributes are pretty standard and used for Data Driven Test cases – rocky May 18 '15 at 21:08
  • @Henrik: Even I dont want to write chained methods. Need is to Load 2 sets of Data in Main Method. and Sub Method A and B are loading it using the Datasource and Table. – rocky May 18 '15 at 21:09

1 Answers1

2

Chaining should only be done if one has to have non-recreate-able data. Otherwise make each test a distinct test.

Data Driven from an XML file.

Consider placing the read-only Xml into a property which is run once before thet tests in the ClassInitialization method. Then test the individual operations, followed by the "Main" operation, Each as a separate testable unit.

public static XDocument Xml { get; set; }

[DeploymentItem("input.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
           "input.xml",
           "User",
            DataAccessMethod.Sequential)]
[ClassInitialize()]
public static void ClassInit(TestContext context)
{ // This is done only once and used by other tests.
    Xml = ...
    Assert.IsTrue(Xml.Node ... );
}

Otherwise look into mocking the data depending on the test being performed or if it comes from a specific call, how about a shim? See my article Shim Saves The Day in A Tricky Unit Test Situation.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • issue with this solution, I have two tables inside my XML file. and each method A and B uses it's own table. My scenarios are Method A and B are distinct test cases, however main method needs A and B as prerequisite to Run further logic. It needs data from both the table of XML file. If I find a way to load data from multiple table then I will remove call to chained method and write independent data load. – rocky May 18 '15 at 20:51
  • @rocky I would look into creating a unique shim or `mocked` values for each unit test. See my article [Shim Saves The Day in A Tricky Unit Test Situation](http://blogs.msdn.com/b/mvpawardprogram/archive/2012/09/04/shim-saves-the-day-in-a-tricky-unit-test-situation.aspx) – ΩmegaMan May 18 '15 at 22:08
  • Thanks. I will try shim and mocking. – rocky May 19 '15 at 19:24