Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.

Slides:



Advertisements
Similar presentations
Automated Testing Ted Driggs (tdriggs). What Verify program behavior without human interaction Programmatically load and run test code on a wide array.
Advertisements

What is Unit Testing? How TDD Works? Tsvyatko Konov Telerik Corporation
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Creating a Program In today’s lesson we will look at: what programming is different types of programs how we create a program installing an IDE to get.
CS 2110 Software Design Principles II Based on slides originally by Juan Altmayer Pizzorno port25.com.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
JUnit, Revisited 17-Apr-17.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
Test-Driven Development With Visual Studio 2005 Erno de Weerd Info Support.
TDD,BDD and Unit Testing in Ruby
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Test Driven Development TDD. Testing ”Testing can never demonstrate the absence of errors in software, only their presence” Edsger W. Dijkstra (but it.
SENG 403, Winter 2012 SENG 403 – Winter  Exploring unit-testing frameworks in.NET  Writing our first test with NUnit  Unit Testing in Visual.
Principles of Object Oriented Programming Practical session 2 – part A.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Testing in Extreme Programming
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
© ALEXANDRE CUVA  VERSION 2.00 Test Driven Design.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
MS Visual Studio 2005 Unit Test. Agenda Team system 概觀 Unit Test Code Coverage Web Test.
Unit Testing Building Rock-Solid Software SoftUni Team Technical Trainers Software University
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
JUnit Don Braffitt Updated: 10-Jun-2011.
M1G Introduction to Programming 2 5. Completing the program.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Object Oriented Analysis and Design 1 Chapter 9 From Design to Implementation  Implementation Model  Forward, Reverse, and Round-Trip Engineering  Mapping.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development.
Unit Testing Building Rock-Solid Software Svetlin Nakov Technical Trainer Software University
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Introduction to Exceptions in Java CS201, SW Development Methods.
Unit testing with NUnit Anne Lam & Chris James CMPS 4113 – Software Engineering April 15, 2015.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Unit Testing.
Building Rock-Solid Software
Software Construction Lab 10 Unit Testing with JUnit
TESTING TEST DRIVEN DEVELOPMENT
Unit testing Java programs Using JUnit
Test Driven Development
Software Testing Software testing.
Syntax, semantics, and pragmatics
Unit testing C# classes
Unit Testing & Test-Driven Development for Mere Mortals
Unit Testing & Test-Driven Development for Mere Mortals
CS 240 – Advanced Programming Concepts
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Junit Tests.
Principles of Object Oriented Programming
Presentation transcript:

Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1

Different kinds of tests Testing levels Unit testing Verify single component Written by developers White-box Integration testing Verify interfaces between components System testing Verify completely integrated system Acceptance testing Users verifies the system Source sting#Testing_levels sting#Testing_levels Testing types Regression testing Re-run old tests to check that old bucks has not come back Alpha testing Internal acceptance test Beta testing External acceptance test Functional vs. non-functional testing Performance testing Usability testing Is the user interface easy to use and understand Security testing Prevent hacking, etc. Source Unit testing C# classes2

Requirements for unit tests Tests must be executable A test must clearly show whether it executed successfully or not The not-so-successful part of the test must not be buried in a pile of test reports. Test code must be separated from the code to be tested We normally don’t want to ship the test code with the production code. Testing is not the same as trying! If you make a main method it’s trying, not testing. Unit testing C# classes3

Unit testing frameworks JUnit is an early and very popular unit testing framework to test Java classes. It has since been ported to many other programming languages, including C# MSTest C# unit testing framework Namespace Microsoft.VisualStudio.TestTools.UnitTesting Annotations [TestClass] marks a class as a test class, supposed to test another C# class [TestMethod] marks a method as a test method A [TestClass] normally holds a number of [TestMethods]’s Other unit testing frameworks for C# xUnit NUnit Unit testing C# classes4

Individual test cases One class (unit) needs one test class (unit test) Annotation [TestClass] Every method (including constructors) needs one or more test methods (test cases) Annotation [TestMethod] Signature of the test method: public void testMethod() Annotations refer to the namespace Microsoft.VisualStudio.TestTools.UnitTesting Example: TryingUnitTesting -> Teacher Unit testing C# classes5

Assert A [TestMethod] should contain a number of calls the static methods from the Assert class Some Assert methods Assert.AreEqual(expected, actual); If the expected is equal to actual the test is green, otherwise red Assert.AreEqual(“Anders”, teacher.Name); Assert.AreEqual(25, teacher.Age); Assert.AreEqual(100.25, teacher.Salary, ); is a so-called Delta: How close do expected and actual have to be Assert.IsTrue(teacher.Salary > ); Assert.Fail(); Will always make the test fail: make the test red Useful when you test exceptions. More on that in a few slides Lots of other Assert methods us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx Unit testing C# classes6

More MSTest annotations to methods [AssemblyInitialize] Executed once when the assembly if first initialized [ClassInitialize] Executed once when the class is initialized, before the first [TestMethod] Useful for opening resources like database or network connections [TestInitialize] Executed before each [TestMethod] Useful for initializing test variables Example: TryingUnitTesting -> TeacherTest [TestMethod] This is the real test. Should contain calls to Assert.xx(…) [TestCleanup] Executed after each [TestMethod] [ClassCleanup] Executed once after the last [TestMethod] Useful for disposing resources like database or network connections [AssemblyCleanup] Executed once after the last [ClassCleanup] Example: TryingUnitTesting -> TestTrace Unit testing C# classes7

How to organize a test: Arrange, Act, Assert pattern Arrange Declare and initialize variables Usually includes an object of the class to be tested Act Call the method to be tested Assert Assert something about the state of the object + the value returned from the method Bill (William) Wake Arrange, Act, Assert (2011) Unit testing C# classes8

What to test? Test all methods that you are not 100% sure that the method is correct One-line methods might not need testing Why not do it anyway; those one-liners are usually easy to test. Methods with if-statements or loops clearly needs testing Unit testing C# classes9

Testing exceptions If a method might throw an exception, you must that that it really does. If the method throws the exception as expected the test is a success. An unexpected exception makes the test fail. There are two ways of testing expected exceptions 1.Using the annotation [ExpectedException(typeof(SomExceptionType))] Requires fewer programming lines Only a single exception can be tested in one test case 2.The try … fail … catch … idiom Requires more programming lines More exceptions can be tested in a single test case You can assert something about the contents of the exception object Example: TryingUnitTesting -> Teacher + TeacherTest Unit testing C# classes10

Making the code testable Sometimes you have to refactor the code to make it more testable. Short methods are easier to test than long methods Methods with a return value are easier to test than void methods You can make assertions about the value returned Methods that has no side-effects (usually declared static) are easy to test Unit testing C# classes11

Help from Visual Studio Visual Studio can assist you in writing unit tests. You make an ordinary project for your ordinary classes. In the same workspace you add a test project Parallel to the ordinary project In this project you make the test classes Running the tests in Visual Studio clearly shows if some tests failed And where the failure is (which lines) Unit testing C# classes12

Generating empty test cases Pre 2012 Visual Studio was able to create empty test cases for a class This feature is now missing! However, you can install a plugin to obtain the same features Unit Test Generator 165b505a38d b505a38d7 Right click a method, and choose “Generate Unit Test” from the context menu OBS the class must be “public” Unit testing C# classes13

Test code coverage Test code coverage tells you which lines in the class the unit test has executed. Normally you want the unit test to examine all the lines of the class to be tested. 100% coverage Visual Studio can show the test code coverage Test -> Analyze Code Coverage Show code coverage colors (small brick like icon a the top of the “Code Coverage Results” panel The file of the class will now show which lines the test executed Green: Executed Red: Not executed Write more test cases to get better coverage Yellow: Some parts of the line was executed Seen with complex conditions involving && or ||, etc. Unit testing C# classes14

Testing in eXtreme Programming (XP) Testing is an important discipline in XP (Extreme Programming) XP idea: Create the test before the code to be tested Writing the test makes you thing about detailed design Test is an executable requirements Writing (and running) test will be a positive experience. We know when a class is done When all tests run Unit testing C# classes15

Test-driven development (TDD) For each new feature in the program: 1.Write (failing) test case 2.Run the test, to see that it fails 3.Write code until the test pass 4.Refactor the code to acceptable standards Source driven_development driven_development Unit testing C# classes16

References and further readings Beck & Gamma JUnit Cookbook, Kent Beck & Erich Gamma invented Junit (the original unit testing framework) Microsoft Developer Networks Verifying Code by Using Unit Tests MSDN Unit Testing and Generating Source Code for Unit Test … Lists and groups Assert methods MSDN TestInitializeAttribute Class Includes an example using the MSTest annotation us/library/microsoft.visualstudio.testtools.unittesting.testinitializeattribute.aspx us/library/microsoft.visualstudio.testtools.unittesting.testinitializeattribute.aspx Bill (William) Wake Arrange, Act, Assert (2011) Extreme Programming, Code the Unit Test First Testing is an important discipline in XP (eXtreme Programming), which is another Kent Bech invention Unit testing C# classes17