繁体   English   中英

在测试方法和测试方法中使用相同的代码

[英]Using the same code in test method and in the method tested

我是TDD开发的新手,我刚刚开始使用Nunit 3.7.1,Newtonsoft.Json版本= 10.0.3,C#和.NET Framework 4.7进行一些测试。

我创建了此测试以测试json反序列化:

[Test]
public void ShouldGenerateBatchExportFile()
{
    string json = @"{""ProductionOrderName"": ""proOrd"",""BatchName"": ""batch_01"",""Codes"": [ --- OMITTED FOR BREVETY --- ]}";
    string path = @"d:\trzlexportsample.json";
    BatchExportFile exportFile = null;

    exportFile = JsonConvert.DeserializeObject<BatchExportFile>(json);

    BatchExportFile generatedFile = _import.LoadBatchFile(path);

    Assert.AreEqual(generatedFile.ProductionOrderName, exportFile.ProductionOrderName);
    Assert.AreEqual(generatedFile.BatchName, exportFile.BatchName);

    Assert.That(generatedFile.Codes, Has.Count.EqualTo(exportFile.Codes.Count));
    Assert.That(generatedFile.Aggregations, Has.Count.EqualTo(exportFile.Aggregations.Count));

    for(int index = 0; index < generatedFile.Codes.Count; index++)
    {
        CodeData gData = generatedFile.Codes[index];
        CodeData testData = exportFile.Codes[index];

        Assert.AreEqual(gData.CodeId, testData.CodeId);
        Assert.AreEqual(gData.Serial, testData.Serial);
        Assert.AreEqual(gData.AggregationLevelId, testData.AggregationLevelId);
        Assert.AreEqual(gData.CommissioningFlag, testData.CommissioningFlag);
        Assert.AreEqual(gData.LastChange, testData.LastChange);
        Assert.AreEqual(gData.UserName, testData.UserName);
        Assert.AreEqual(gData.Source, testData.Source);
        Assert.AreEqual(gData.Reason, testData.Reason);
    }

    for (int index = 0; index < generatedFile.Aggregations.Count; index++)
    {
        AggregationData gData = generatedFile.Aggregations[index];
        AggregationData testData = generatedFile.Aggregations[index];

        Assert.AreEqual(gData.AggregationId, testData.AggregationId);
        Assert.AreEqual(gData.Parent, testData.Parent);
        Assert.That(gData.Children, Has.Count.EqualTo(testData.Children.Count));

        for (int j = 0; j < gData.Children.Count; j++)
        {
            AggregationChildrenData gChildren = gData.Children[j];
            AggregationChildrenData testChildren = testData.Children[j];

            Assert.AreEqual(gChildren.Serial, testChildren.Serial);
            Assert.AreEqual(gChildren.Serial, testChildren.Serial);
        }
    }
}

这是我正在测试的方法:

public BatchExportFile LoadBatchFile(string path)
{
    if (string.IsNullOrWhiteSpace(path))
        throw new ArgumentNullException(nameof(path));

    BatchExportFile exportFile = null;

    using (StreamReader file = File.OpenText(path))
    {
        JsonSerializer serializer = new JsonSerializer();
        exportFile = (BatchExportFile)serializer.Deserialize(file, typeof(BatchExportFile));
    }

    return exportFile;
}

文件D:\\trzlexportsample.json具有与string json相同的内容。

我不确定我是否以正确的方式进行测试,因为在测试中,我使用的代码与_import.LoadBatchFile(path)方法中使用的代码大致相同

在测试中,我有以下代码:

exportFile = JsonConvert.DeserializeObject<BatchExportFile>(json);

在测试方法中,我有以下代码:

using (StreamReader file = File.OpenText(path))
{
    JsonSerializer serializer = new JsonSerializer();
    exportFile = (BatchExportFile)serializer.Deserialize(file, typeof(BatchExportFile));
}

它们基本相同。

我的方法正确吗?

换句话说,在测试和测试的方法中使用相同的代码是否正确?

尽管您使用的是相同的代码,但是您将其用于不同的目的:

  • 您的代码使用JSON反序列化器反序列化“有效载荷”字符串,
  • 您的测试代码使用JSON反序列化器构造一个对象,将针对该对象测试您要测试的程序返回的对象。

如果您正在测试JSON序列化程序代码本身,这将是有问题的。 但是,您使用的JSON序列化程序库值得信赖,可以做正确的事情,因此您的方法是完全可以接受的。

显然,另一种方法是根本不构造exportFile ,而用常量字符串替换对其成员的引用,例如

Assert.AreEqual(generatedFile.ProductionOrderName, "actual-order-name");
Assert.AreEqual(generatedFile.BatchName, "actual-batch-name");
... // And so on

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM