In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1 unit test
Your RP is determined by the quality of your program.
-- A Chao
This chapter is about the cooperation between two people. since the program is written by two people, there will be a situation in which a module written by one person is called by a module written by another person. Many misunderstandings and negligence occur between the two modules. How can you make your module as unassailable as possible? Unit testing is a very effective solution.
1.1 write unit tests with VSTS
Example: let's write a more commonly used type and see how its unit tests should be written. For example, the type of "user" is used in all kinds of website applications. Who volunteered to perform and write the code? Xiao Fei, OK, please come on stage.
Xiaofei created a class library (Class Library) for C # and wrote code such as listing 11-1:
Listing 11-1
Namespace DemoUser {public class User {public User (string userEmail) {m_email = userEmail;} private string masked email; / / user email as user id}} OK, now right-click User to see the "Create Unit Tests" menu, so you can create a new unit test (see figure 11-2).
Figure 11-2 create a unit test project
After creating the unit test, notice that three new files appear in Solution Explorer (shown in figure 11-3).
Figure 11-3 New unit test file
Class1.cs is the file of the program, and Class1Test.cs is the corresponding unit test file.
DemoUser.vsmdi: test management files.
Localtestrun.testrunconfig: local test run settings file.
How do you manage the settings file? It is not correct to right-click the Property. You have to double-click the file to enter the management and settings interface. In the setup interface, you can have the unit test generate a "demouser.dll" code coverage report.
Note that in the unit test, VSTS automatically generates the test skeleton for you, but you still have to do a lot of things yourself, at least those / / TODO things (see listing 11-2). At this time, unit tests still use Assert. Inconclusive, indicating that this is an unvalidated unit test.
Code listing 11-2
/ A test for User (string) / / [TestMethod ()] public void ConstructorTest () {string userEmail = null; / / TODO: Initialize to an appropriate// value User target = new User (userEmail); / / TODO: Implement code to verify targetAssert.Inconclusive ("TODO: Implement code to verify target");} after a simple modification, we get a formal unit test such as listing 11-3:
Listing 11-3
[TestMethod ()] public void ConstructorTest () {string userEmail = "someone@somewhere.com"; User target = new User (userEmail); Assert.IsTrue (target! = null);} / / We can further test whether E-mail is indeed a structure that interprets unit tests in the User type
From the above example, you can see the main steps in creating a unit test function:
(1) setting data (a hypothetical correct E-mail address)
(2) use the function of the type being tested (create an entity of the User class with an E-mail address)
(3) compare the actual results with the expected results (Assert.IsTrue (target targets = null).
Now you can run the unit test, and take a look at the code coverage report "code coverage report". The code is 100% covered.
Of course, at this time, there are still a lot of cases in the code that have not been dealt with, and the students say miscellaneous things off the stage.
Dealing with empty strings, strings with zero length, are all empty strings.
Xiaofei skillfully wrote the following three tests in Copy/Paste, as shown in listing 11-4.
Code listing 11-4
[TestMethod ()] [ExpectedException (typeof (ArgumentNullException))] public void ConstructorTestNull () {User target = new User (null);} [TestMethod ()] [ExpectedException (typeof (ArgumentException))] public void ConstructorTestEmpty () {User target = new User ("") } [TestMethod ()] [ExpectedException (typeof (ArgumentNullException))] public void ConstructorTestBlank () {User target = new User ("");} if the code in the class library is not modified, the unit test reports that all three new tests have failed.
Xiaofei made changes to the code accordingly. The result is an error like this, as shown in listing 11-5:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.