1

我正在为 Web 服务测试编写单元测试。我添加了对 C# 单元测试解决方案的服务引用,并开始使用 Web 服务中的类进行测试。我还添加了一个 Excel 文件来提供单元测试值。

下面是一个关于我之前在做什么的例子

[DataSource("System.Data.Odbc"
 ,"Dsn=Excel Files;dbq=|DataDirectory|\\TestData.xlsx;defaultdir=C:\\TestData;driverid=1046;maxbuffersize=2048;pagetimeout=5"
 ,"Sheet1$"
 ,DataAccessMethod.Sequential)
 ,DeploymentItem("TestProject1\\TestData.xlsx")
 ,Owner("")
 ,Description("")
 ,TestMethod()]
public void test1()
{
    try
    {
        var Service = new Service.ServiceClient();
        var Cid = testContextInstance.DataRow["CId"].ToString();
        var MNumber = testContextInstance.DataRow["MNumber"].ToString();
        var VID = testContextInstance.DataRow["VID"].ToString();
        var isVisit = new Service.ISVisit()
        {
            CID = Cid,
            MNum = MNumber,
            VCode = VID
        };

    var first = Service.Medis(isVisit).Cast<Service.ISMedi>().FirstOrDefault();
    // Assert
    Assert.AreEqual("12345678", first.Proc.ProcID);
    }
    catch (Exception ex1)
    {
        if (ex1.InnerException != null)
            Debug.WriteLine(ex1.InnerException.Message);
        Assert.Fail(ex1.Message);
    }
}

此 test1 已通过.Medis()IsVisit是服务中的类。到目前为止,我在 Excel 文件中只有一行,我能够检索数据并对其进行测试。

enter image description here

但是现在的要求是,如果excel文件中的行多于一行,单元测试应该循环遍历每一行并测试每一行以查看通过/失败。

enter image description here

Please Help me with this.How to loop through each row in the excel file in and check the test results.How to handle this situation in c# unitesting.Thank you

4

1 回答 1

1

You are already doing it, DataSource attribute runs test once per row in source file. Your problem is asserting results.

To solve it, your excel files should also contain some sort of expected result for row column (ProcId perhaps), which you'll retrieve together with row data and assert against at the end:

// ...
var VID = testContextInstance.DataRow["VID"].ToString();
var expectedResult = testContextInstance.DataRow["ExpectedResult"].ToString();
var isVisit = new Service.ISVisit()
{
    CID = Cid,
    MNum = MNumber,
    VCode = VID
};

// ...

Assert.AreEqual(expectedResult, first.Proc.ProcID);
于 2012-10-08T21:19:04.973 回答