How to write a test method in NUnit to test the savefile method?

I wrote a savefile method to save an object in xml. But I'm not sure how to test a method in NUnit. Do I need to create a sample file manually and compare a line between files? Are there any better ways to test a method?

Thanks for your reply.

+4
source share
5 answers

Ugh checking XML output. Welcome to Hell:)

The method that worked for me was to create an XmlDocument object in memory. Then run the SaveFile method and load it into another XmlDocument . Pass both XmlDocuments recursively, comparing all elements and attributes.

Unfortunately, the xml entry with C # objects is large and voluminous. I would recommend testing subsections at the same time. Perhaps your save file has a helper element <FileList> , a subelement <Cups> and a subelement <Rifles> . In this case, write a series of tests to make sure that each subheading is executed correctly, and not the overall conclusion.

+1
source

It would be easier if you showed the code. My way around this is to add an abstraction layer. Do not use the Save method directly in XmlWriter. Instead, create wrappers, each of which will be able to save a small logical fragment of your data in xml and test them.

for each wrapper have a method such as

 void Persist(XmlWriter writer); 

and let Save just collect the data from the wrappers. In other words, the responsibility for data storage should be the responsibility of the Wrappers, but each of them will be responsible for a small piece of it. There you can test it by comparing strings (put StringWriter in XmlWriter, which you pass to Persist method)

+1
source

Options

  • Simplified: use the Golden File approach. Create the expected output file as a resource. Make the byte / line a wise comparison with the actual generated file.
  • I heard that some people use XmlUnit for this purpose. Never used it personally, but maybe it's worth a look at
+1
source

Here's what I do - it works for me, but it may not be according to your specifications.

In the passage I make sure that I delete any files that I could create as part of the package. So yes, make sure the file is a scratch file for testing

In the testing method, I delete the file if it exists (it should not, since the trimming has already taken care of this), and then output the file to XML, and then confirm that the file exists. Then I reload the file either in the object graph or in the DOM xml and request the state through as many statements as you need.

If you can get away with writing to files, and your design allows you to write to a common text script or XML writer, you can bypass file saving and replace calls with a string writer and just request a string. Much cleaner, but it does not check the operability of the actual file.

0
source

Create your class so that you have SaveFile (string fileName) and SaveFile (XmlWriter writer). SaveFile (string filename) you only need to create an XmlWriter for the named file and call SaveFile (writer). Do extensive unit testing in the SaveFile (XmlWriter writer) method, for which you can use mock XmlWriter. Check out some error conditions — if you plan to process them rather than distributing them — in the SaveFile (string filename) method. Depending on the error handling, you may not need to create a file at all.

By the way, you do not need to expose your method using XmlWriter if you do not want to. You can make this confidential and use the accessory to challenge it in your tests.

0
source

All Articles