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

Slides:



Advertisements
Similar presentations
Software Architecture Prof.Dr.ir. F. Gielen
Advertisements

Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Applied Software Project Management Andrew Stellman & Jennifer Greene Applied Software Project Management Applied Software.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
CASE Tools And Their Effect On Software Quality Peter Geddis – pxg07u.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
Understand Application Lifecycle Management
‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7.
Level 2 IT Users Qualification – Unit 1 Improving Productivity Chris.
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
Version Control.
From Quality Control to Quality Assurance…and Beyond Alan Page Microsoft.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Version Control and SVN ECE 297. Why Do We Need Version Control?
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Unit Testing by Jon Edgar. Structure of Presentation Structure What is Unit Testing? Worked Example Extreme Programming (XP) Implementation Limitation.
Head First Python: Ch 3. Files and Exceptions: Dealing with Errors Aug 26, 2013 Kyung-Bin Lim.
Northwest Arkansas.Net User Group Jay Smith Tyson Foods, Inc. Unit Testing nUnit, nUnitAsp, nUnitForms.
Here’s Why You Should Choose Website Builders over Other Options Squarespace allows you to add and move around your content (text, video, images, sounds,
TDD Unit tests from a slightly different point of view Katie Dwyer.
Exclusive Business to Business CRM
Dial-In Number: 1 (631) Webinar ID: FHC Tech Talk Automation and Efficiency Series Talk #1 Carbonite automated backup.
Objection Handling. Agenda Seven Steps to handle objections 10 Common objections Questions.
Software Testing Kobla Setriakor Nyomi Faculty Intern (Programming II)
Leading CRM for Educational Institutions
CSC 108H: Introduction to Computer Programming
FOP: Multi-Screen Apps
Summary prepared by Kirk Scott
Test Granularities Unit Testing and Automation
Continuous Integration (CI)
Sample Wiki Comments?.
Introduction to Python
Logger, Assert and Invariants
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Brian Leonard ブライアン レオナルド
TDD Overview CS 4501/6501 Software Testing
Week 14 - Wednesday CS 121.
Applied Software Implementation & Testing
FAST Administration Training
Unit testing C# classes
Ordering Healthy Food Online
Introducing Automation in Traditional Software Testing Best Practices.
You know that customers are the life-blood of your small business, and that making your customers happy is what keeps them coming back to you again and.
Why Technology Startups Should Not Ignore Software Testing.
Marketing automation is a very active segment of the CRM software market. Not only are new vendors entering the market, but existing market leaders are.
Mr Barton’s Maths Notes
Design and Programming
Common C Programming Errors, GDB Debugging
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
UNITY TEAM PROJECT TOPICS: [1]. Unity Collaborate
Please use speaker notes for additional information!
Automating Profitable Growth
CSCE 489- Problem Solving Programming Strategies Spring 2018
CS240: Advanced Programming Concepts
Get In Shape With EMS Training. INTRODUCTION Those that are thinking about making a change in their life might have thought about going through with EMS.
Introduction When searching for a new mattress, you have to make sure you know where to go to find the best one. The mattress you sleep on is going to.
Why CRM Doesn’t Work as Partner Management Software
Git started with git: 2018 edition
Automating Profitable Growth™
Applying Use Cases (Chapters 25,26)
Applying Use Cases (Chapters 25,26)
Polymorphism 2019/4/29.
Booksy University Bug Reports and Feature Requests.
You’ll get better code in less time (If you do it for a while)
Test Cases, Test Suites and Test Case management systems
Software Development Techniques
Types of Software Testing Course. CONTENT  Black-box testing course  White-box software testing course  Automated software testing course  Regression.
Presentation transcript:

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

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.

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…

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.

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.

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!

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.

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

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

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.

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…

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); }

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); }

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.

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.

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); }

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.

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:

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.

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.

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.