Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit testing Why test your code when your code can test it for you?

Similar presentations


Presentation on theme: "Unit testing Why test your code when your code can test it for you?"— Presentation transcript:

1 Unit testing Why test your code when your code can test it for you?

2 What is Unit testing? Unit testing is form of automated software testing. Typically, Unit tests are housed within a single project within the solution that the tests are targeting. Each Unit test will usually focus on one function. Each Unit test will attempt to call that function with a predefined input and then verify whether or not the expected outcome has occurred.

3 Why should I bother with Unit testing? It sounds a bit complicated? Why would I want to write extra code that doesn’t even add functionality? Can’t I can already test my software by just using it? Let’s imagine a scenario…

4 Hypothetical Project You’ve just been hired out of school by a software company. Woohoo! Money! For your first project, they’ve got you working on one of their older applications. It’s be around for quite a while. Some of the developers who have worked on it in the past aren’t even with the company any longer. Your boss has given you a small list of new features to add on.

5 Hypothetical Project cont’d You dive right in to the source. You realize that you don’t understand exactly what the various areas of the code actually accomplish. You begin writing code for the new features and eventually you reach the point where your new code has to interface with the existing old code. You merge the two as carefully as you can. Your new features are working great, and as far as you can tell, nothing seems broken. So you push an update out to the users. Your users report some that functionality is broken. The program crashes in unexpected ways. Everyone is now mad at you. You lie down and try not to cry. You cry a lot.

6 Hypothetical Project cont’d What happened? The project would build fine without any errors? Doesn’t that mean it works? What about the project made it difficult to detect those errors? Is there any way the project as a whole could have been structured to help prevent pushing out a software update with core functionality broken? Yes, with Unit testing!

7 How does Unit testing work? Now that we can visualize the difficulty of managing a large code base, how does Unit testing exactly help us do that? As stated before, Unit testing allows developers to target individual pieces of the overall project, test them with predefined input, and verify that the correct outcome has occurred. The more pieces of our code base we can create tests for, the more of our code base we can quickly validate after new changes.

8 Adding Unit testing to your application To add a Unit testing project, simply right click on your solution and select Add -> New Project.

9 Adding Unit testing to your application cont’d Find the Unit Test Project template and name it something appropriate, like “APPLICATION_NAMEUnitTests”.

10 Adding Unit testing to your application cont’d Lastly, you’ll need to make sure your Unit test project references your main project, the one you’ll be testing.

11 Adding Unit testing to your application cont’d [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { } Well, it made…..something? How exactly do we use this test method? Let’s look at some tests that actually accomplish something…

12 Example #1 [TestClass] public class ActivateInventoryTests { [TestMethod] public void Test_That_Inventory_Gets_Activated() { //--Arrange Inventory inventory = new Inventory(1, 27, "Monitor"); //--Act InventoryLogic.ActivateInventory(inventory); //--Assert Assert.IsTrue(inventory.Active); }

13 Example #2 [TestClass] public class GetInventoryNotesTests { [TestMethod] public void Test_That_Method_Returns_Valid_Object() { //--Arrange IEnumerable notes; //--Act notes = InventoryLogic.GetInventoryNotes(true, "Basic") as IEnumerable ; //--Assert Assert.IsNotNull(notes); }

14 Running your Unit tests To run your tests, simply select TEST -> Run -> All Tests. Visual Studio will find all tests decorated with our TestClass and TestMethod attributes and run them.

15 Running your Unit tests cont’d For each of our tests, Visual Studio shows a pass or fail. Also, on subsequent test runs, Visual Studio gives you the option to only run failed tests, allowing you to focus on those if you desire.

16 What’s problematic with this? static void Main(string[] args) { if (args[1] == "create") { if (args[2] == "customer") { if (args[3] == "international") { Customer c = new Customer(); c.Name = args[4]; CustomerDao.SaveCustomer(c); } else if (args[3] == "domestic") { Customer c = new Customer(); c.Name = args[5]; CustomerDao.SaveCustomer(c); } } else if (args[1] == "update") { if (args[2] == "customer") { int id = -1; int.TryParse(args[3], out id); Customer c = CustomerDao.GetCustomerByID(id); c.Name = args[4]; CustomerDao.SaveCustomer(c); }

17 Programming with Unit tests in mind Unit tests are NOT something you can throw in at the very end. You code must be designed from the ground up to be testable. It won’t happen by accident. You must plan for it. There are several things to consider when designing your code to be testable: Focus on classes that handle one thing, or in rare cases a small group of things, well. If you’ve got one class for dealing with customers, and only that class deals with customers, Unit tests for that class and its methods are more likely to guarantee the validity of functionality regarding that class overall. Try not to rely on static variables or methods. Since these can potentially be called or modified anywhere, they can add uncertainty to the closed environment our Unit tests rely on.

18 Programming with Unit tests in mind cont’d Classes with overly complicated initialization can hinder Unit testing. The constructer should always be the only method you need to call before you can start using your class. If this is not the case and your class requires additional setup depending on how it is being used, consider whether or not your class is focused enough or if it should be split up. How to write testable code could fill an entire seminar on it’s own. If you want to learn more, there are resources online to help sharpen that skill. Miško Hevery’s Guide: Writing Testable Code is a great place to start: http://tinyurl.com/69kou2 http://tinyurl.com/69kou2

19 Disclaimers! Even as great as Unit tests are, we need to keep them in perspective. Unit testing is not a replacement for traditional testing through the actual software interface. Just like the idea that no compilation errors does not mean no bugs, passing every Unit test you’ve written does not mean no bugs either. Passing your Unit tests simply means that your core code is functioning properly. The more exotic functions of the code that can’t easily be verified with a Unit test or the areas that really heavily on the actual interface still must be tested the old fashioned way.

20 Benefits of Unit testing Basic testing of the application can be performed extremely fast. Once you’ve taken the initial steps to write the tests, they can be performed at any time with a few clicks. When testing is that easy, you’re naturally going to do it more often and catch more mistakes. It makes regression testing easier. This is evidenced by our hypothetical example from before. With Unit tests, you can have a much greater degree of confidence that new changes have not broken existing functionality. You’ll save time for your testers. It can get a bit maddening when things that were working break in strange ways and now need completely validated all over again.

21 Benefits of Unit testing cont’d Unit tests are code, so they can be versioned with tools like Subversion or Git. They will be shared with anyone who also has access to repository the source code is in. When working with a team, everyone will have access to all of the Unit tests that the other team members have written. This means that you can begin testing other team members code even before you’ve read it. As long as tests exist for their code, you can verify that it is working properly.


Download ppt "Unit testing Why test your code when your code can test it for you?"

Similar presentations


Ads by Google