We discussed the RSS feed for the UWP in the posture, and stored it locally as a file, and then converted the data into a Model object. This part of the non-UI content is ideal for adding Unit Test. Without the UI ,UT is simple and efficient, and it is worthwhile to invest a little time to ensure the reliability of the program.
The uwp Unit Test is not complex to create, first, under the Posture UWP solution, to create and Zhangzishirssread Engineering-PEER UT Engineering: Zhangzishirssread.uttest.
Click OK to create a new ut engineering, structure and general uwp The project is basically consistent, and will automatically include zhangzishirssread project references, and will create unittestapp Such entry files, as well as for writing test case Span lang= "en-US" >unittest1.cs file, rename the file (usually a utunittestfilestorehelper filestorehelper.test.cs We are here because all test case are written in a file, the filename is called unittest.cs
The class file used for the Unit Test is the same as the normal class, and the only difference is the need to add [TestClass] attribute to identify it. Each test case that also needs to run the tests is also added to the [TestMethod] attribute.
[TestMethod] Public async Task Testsavefile (sbsbo.cn) { var filestorehelper = new Filestorehelper (); var RssReader = new RssReader (); var result = await rssreader.downloadrssstring (); var issuccess = await filestorehelper.saverssfileasync (result); Assert.istrue (issuccess); } [TestMethod] Public async Task Testreadfile () { var filestorehelper = new Filestorehelper (); var RssReader = new RssReader (); var result = await rssreader.downloadrssstring (); await filestorehelper.saverssfileasync (result); var content = await Filestorehelper.readrssfileasync (chuangshi88.cn); Assert.areequal<string> (result, content); }
After you have written UT, you can choose to run all the test cases from the menu bar's "test", which opens the test explorer on the left, which shows the Pass and Fail tests Case, stack information, and so on.
It is important to note that our APP may use some files in the Asserts directory, to ensure that the Test project has the same resources, otherwise it may not be compiled through.
If, after running the test, you find that the individual tests did not pass, you can also select the failed tests in the test Explorer, and then click on the "Test" menu, "debugging", "Selected tests" for single-step debugging, set breakpoints to determine the cause of the problem, Just like the normal debug .
Sometimes encountered some object or data, in the UT environment can not get, generally there are two methods. One is to produce false data by creating a Fake class that provides false classes and methods of the same name. The other is to modify the interface of the class or method to be tested, and when the class instance is created, the data is passed in from outside.
usually a test caseasserttest caseareequalassertisfalse,isnull
In the up -pose UWP , I added testdownloadrss,testparserssxml,testsavefile and testreadfile these four test casfanku1.come, by testing the public method of RssReader and Filestorehelper two classes , This overrides the private method inside the class .
This article provides a brief introduction to Unit testing in the UWP, so you can look at the code if you are interested.
GitHub Source code address:
Https://github.com/manupstairs/ZhangZiShiRSSRead
UWP Source--unit Test