简体   繁体   中英

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). After passing the parameters I'm getting a rtfString from this method. 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.

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 . 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

Nunit Testing

XUnit Testing

In the end you can use Assert To do the right amount of the desired.

  Assert.NotNull(output)

or

  Assert.Equal(Expected,output) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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