(Basic) "Are you really the Tony Stark I know? May I test you first?"
Bài đăng này đã không được cập nhật trong 3 năm
Background
We've already met this guy in my previous article. But is he really the Tony Stark we know? Let's make him answer some questions first, ok.
Before we begin
I just want you to know that in this article I was using Visual Studio (VS) 2013, so for any other version of VS the screenshots might be a little bit different.
This is the procedure, Tony, you know that
This article will show you guys how to create your first unit test. So, just follow the rabbit, err, I mean the tutorial here step by step.
1. Create the project
I guess all of you have ever created a VS project, once in your lifetime. So, it won't be any different here. This time, let's pick up a Unit Test Project, as you can see below :
2. Add a unit test to the project
Beautiful! When the project is ready, just right click on it in the Solution Explorer (yes, there.. the rightmost toolbox by default), and then please choose Add->Unit Test to add your first unit test file in the project :
3. Let's get to the test script!
Now is the time to fire some questions to Tony Stark, to make sure that he's the man we used to know :
[TestClass]
public class UnitTest1
{
[TestMethod]
public void AreYouTonyStark()
{
Human tonyStark = new Human(1, "Tony Stark", 30, 50, 170);
Assert.AreEqual("Tony Stark", tonyStark.Name); //Are you Tony Stark?
Assert.AreEqual(30, tonyStark.Age); //Are you 30 years old?
Assert.AreEqual(50, tonyStark.Stamina); //Is your stamina equals 50 points?
Assert.AreEqual(170, tonyStark.Intelligence); //Is your IQ as high as 170 points?
Assert.AreEqual(100, tonyStark.HealthPoints); //Are you fully healthy?
}
}
Compile this project right after you finish writing this script.
Do you notice the TestClass and TestMethod attributes right there? Do you know that those attributes must be there in order to make it available in the Test Explorer? You know it? Hmm, great then. Perfect!
4. Find the first unit test available
Now you need to see to the leftmost of your screen. Can you see that? The Test Explorer there? Just click it and you will see your first available unit test right over there :
5. Result from our first unit test
If you can find a clickable text "Run All" right there, please don't be shy to touch it, because this will trigger the unit test for you. Right after it finish, VS will back with the result, just like this screenshot below :
And as you can see here, he IS the Tony Stark we know. Yeeaaaay..! Welcome, Tony!
Unit test is ALWAYS important
###Yes it is! Because the test result IS the guarantee, not your words. Even if you think you know it better. You don't want anybody knows about your sloppiness, right?
Even if you know Tony Stark better, in this case. Remember, in this universe where a shapeshifter like Mystique is roaming around freely out there, you cannot be sure of anything. Just do the procedure, ok?
"Ok, boss! By the way, who is Mystique?"
Ok, now what?
Care to try another Unit Test engine?
All rights reserved