Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a null value to a data driven unit test from a CSV file?

I have a unit test written in c# that uses a .CSV as a datasource:

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestData.csv", "TestData#csv", DataAccessMethod.Sequential), DeploymentItem("TxRP.Tests\\TestData.csv"), TestMethod()]
    public void CompareOrgsTest()
    {
        // Arrange
        var vdd = new Mock<ViewDataDictionary>().Object;
        HtmlHelper helper = MVCMocks.CreateMockHelper(vdd);
        string orgOne = testContextInstance.DataRow["OrgOne"].ToString();
        string orgTwo = testContextInstance.DataRow["OrgTwo"].ToString();
        bool expected = Convert.ToBoolean(testContextInstance.DataRow["OrgCompareExpected"]); 

        // Act
        bool actual = HtmlHelpers.CompareOrg(helper, orgOne, orgTwo);

        // Assert
        Assert.AreEqual(expected, actual, "Did not return " + expected + ".  Org1=" + orgOne + ", Org2=" + orgTwo);
    }

Works fabulously, until I needed to add some null value testing. I can't seem to figure out how to pass a NULL value as one of the data elements...has anyone done this before?

Thanks!

like image 925
morganpdx Avatar asked Nov 24 '10 20:11

morganpdx


1 Answers

You can't. You're reading in values from a CSV file. There is no such thing as a 'null' value in a CSV file. The next best thing I know to do would be to write some code to test 'null' if a 'magic string' is read from the CSV file.

string orgOne = testContextInstance.DataRow["OrgOne"].ToString();
if (orgOne=="null")
    orgOne = null;
like image 174
Vivian River Avatar answered Oct 29 '22 19:10

Vivian River