简体   繁体   English

如何对文件或文档进行单元测试?

[英]How to unit test a file or document?

First of all I'm all new to testing and apologize if this is an inappropriate question. 首先,如果这是一个不合适的问题,我将不熟悉它,并表示歉意。

In the project I'm working on there is a method that creates a document(.RTF). 在我正在研究的项目中,有一种创建文档(.RTF)的方法。 After passing the parameters I'm getting a rtfString from this method. 传递参数后,我从此方法获取rtfString。 What I need test is the page numbering of the created document. 我需要测试的是所创建文档的页码。 Since the output of this method is a string, what I'm doing is converting that into a document which is stored on the my machine. 由于此方法的输出是字符串,因此我正在将其转换为存储在我的计算机上的文档。 Then I manually open the document and check for the numbering. 然后,我手动打开文档并检查编号。

This is the code I use to convert the string to document. 这是我用来将字符串转换为文档的代码。 output is the rtfString. 输出是rtfString。

string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testHtmlToRtf.rtf");
if (System.IO.File.Exists(outputPath))
{
    System.IO.File.Delete(outputPath);
}
System.IO.File.WriteAllText(outputPath, output);

I have a feeling that this is not a good testing practice. 我感觉这不是一个好的测试实践。 But I don't know what else to do. 但是我不知道该怎么办。 Please can anyone give me some advice or good tutorial regarding this matter. 请任何人就此问题给我一些建议或好的教程。

This is my test case. 这是我的测试用例。 But I don't think it's usefull. 但是我认为这没有用。

public void PageNumberingTest()
{
    //Arrange
    string htmlContent = "<div>This is test content.</div>";
    string header = "<div>test header</div>";
    string footer = "<div>test footer</div>";

    //Act
    var output = _rtf.ConvertToString(htmlContent, header, footer);

    //Assert
    //To run the tests uncomment following code
    string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testHtmlToRtf.rtf");
    if (System.IO.File.Exists(outputPath))
    {
        System.IO.File.Delete(outputPath);
    }

    System.IO.File.WriteAllText(outputPath, output);
    output.ShouldNotBeNull();
} 

In cases where your app is producing a file based on a standard it is best to open it up in another app of a known quantity . 如果您的应用基于标准生成文件,则最好在已知数量的另一个应用中打开该文件。 Otherwise you are simply writing a test which you don't know is correct or not, to test an artefact which you don't know is correct or not. 否则,您只是在编写不知道是否正确的测试,以测试您不知道正确或不正确的人工制品。 There are too many unknowns. 有太多未知数。

Since it is RTF, it makes sense to view the file in an established app whether it be WordPad or Microsoft Word . 由于它是RTF,因此无论是WordPad还是Microsoft Word都可以在已建立的应用程序中查看文件。 This is something I have done in the past and worked quite well. 这是我过去所做的,并且效果很好。

The same can be said if you are developing against a protocol - sure you can assert that the bytes seem to be right, but until you try talking to another app you won't know for sure. 如果您是根据协议进行开发的话,也可以这样说-确保可以断言字节似乎正确,但是在尝试与另一个应用程序交谈之前,您可能并不确定。

I have a feeling that this is not a good testing practice. 我感觉这不是一个好的测试实践。

Some tests are manual and are either too difficult or not possible to test automatically. 有些测试是手动的 ,太难了或者无法自动测试。 For this, writing unit test code is arguably unwise and should be replaced with manual testing with a known app as I said. 为此,编写单元测试代码可能是不明智的,应使用我所说的已知应用程序进行手动测试来代替。

You can use it to test this 你可以用它来测试

MsTest MSTEST

Nunit Testing Nunit测试

XUnit Testing XUnit测试

In the end you can use Assert To do the right amount of the desired. 最后,您可以使用断言来正确执行所需的量。

  Assert.NotNull(output)

or 要么

  Assert.Equal(Expected,output) 

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

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