我是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)
中使用的代码相同<\ n} /强>
在测试中我有这段代码:
exportFile = JsonConvert.DeserializeObject<BatchExportFile>(json);
在测试方法中,我有这段代码:
using (StreamReader file = File.OpenText(path))
{
JsonSerializer serializer = new JsonSerializer();
exportFile = (BatchExportFile)serializer.Deserialize(file, typeof(BatchExportFile));
}
它们大致相同。
我的方式是否正确?
换句话说,在测试和测试方法中使用相同的代码是否正确?
答案 0 :(得分:2)
虽然您使用相同的代码,但您将其用于不同目的:
如果您正在测试JSON序列化程序代码本身,则会出现问题。但是,您正在使用您信任的JSON序列化程序库来做正确的事情,因此您的方法是完全可以接受的。
显然,另一种方法是根本不构造exportFile
,并用常量字符串替换对其成员的引用,例如。
Assert.AreEqual(generatedFile.ProductionOrderName, "actual-order-name");
Assert.AreEqual(generatedFile.BatchName, "actual-batch-name");
... // And so on