简体   繁体   English

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

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

I'm new in TDD developing and I've just started to do some tests with Nunit 3.7.1, Newtonsoft.Json version=10.0.3, C# and .NET Framework 4.7. 我是TDD开发的新手,我刚刚开始使用Nunit 3.7.1,Newtonsoft.Json版本= 10.0.3,C#和.NET Framework 4.7进行一些测试。

I have created this test to test a json deserialization: 我创建了此测试以测试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);
        }
    }
}

And this is the method I'm testing: 这是我正在测试的方法:

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;
}

The file D:\\trzlexportsample.json has the same content than the string json . 文件D:\\trzlexportsample.json具有与string json相同的内容。

I'm not sure if I'm doing the test in the right way because in the test I'm using mostly the same code than in the method _import.LoadBatchFile(path) . 我不确定我是否以正确的方式进行测试,因为在测试中,我使用的代码与_import.LoadBatchFile(path)方法中使用的代码大致相同

In the test I have this code: 在测试中,我有以下代码:

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

And in the test method I have this code: 在测试方法中,我有以下代码:

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

They are mostly the same. 它们基本相同。

Is the way I do it correct? 我的方法正确吗?

In other words, is it correct to use the same code in the test and in the method tested? 换句话说,在测试和测试的方法中使用相同的代码是否正确?

Although you are using the same code, you are using it for different purposes: 尽管您使用的是相同的代码,但是您将其用于不同的目的:

  • Your code uses JSON deserializer to deserialize the "payload" string, 您的代码使用JSON反序列化器反序列化“有效载荷”字符串,
  • Your test code uses JSON deserializer to construct an object against which you are going to test the object returned by the program that you are testing. 您的测试代码使用JSON反序列化器构造一个对象,将针对该对象测试您要测试的程序返回的对象。

This would be problematic if you were testing JSON serializer code itself. 如果您正在测试JSON序列化程序代码本身,这将是有问题的。 However, you are using JSON serializer library that you trust to do the right thing, so your approach is perfectly acceptable. 但是,您使用的JSON序列化程序库值得信赖,可以做正确的事情,因此您的方法是完全可以接受的。

Obviously, another approach would be to not construct exportFile at all, and replace references to its members with constant strings, eg 显然,另一种方法是根本不构造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