Unit testing C# classes

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
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
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.
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
Equivalence Partitioning Identify the inputs, behaviors, or other factors that you want to test based on the functionality and specifications Group these.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.
TDD,BDD and Unit Testing in Ruby
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
SENG 403, Winter 2012 SENG 403 – Winter  Exploring unit-testing frameworks in.NET  Writing our first test with NUnit  Unit Testing in Visual.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
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
Code Correctness, Readability, Maintainability Telerik Software Academy Telerik School.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
© ALEXANDRE CUVA  VERSION 2.00 Test Driven Design.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
By for Testing Tools: Test Automation and supporting tools Jariro Pava, Robert Vanderwall 1 WISTPC-14.
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
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.
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.
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,
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
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”
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Northwest Arkansas.Net User Group Jay Smith Tyson Foods, Inc. Unit Testing nUnit, nUnitAsp, nUnitForms.
Software Engineering Lecture 11 Software Testing Presenter: Josef Hallberg 1.
Unit testing Why test your code when your code can test it for you?
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
CSC 108H: Introduction to Computer Programming
Unit Testing.
Building Rock-Solid Software
Software Construction Lab 10 Unit Testing with JUnit
Unit Testing - solid fundamentals
Test-driven development
TESTING TEST DRIVEN DEVELOPMENT
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2014
CIT 590 Unit testing.
Unit testing Java programs Using JUnit
Test Driven Development
Computer Programming I
Software Testing Software testing.
Syntax, semantics, and pragmatics
Unit Testing & Test-Driven Development for Mere Mortals
White-Box Testing Using Pex
Unit Testing & Test-Driven Development for Mere Mortals
Test-first development
Automated Testing Environment
Testing and Test-Driven Development CSC 4700 Software Engineering
Unit Testing & Test-Driven Development for Mere Mortals
CS 240 – Advanced Programming Concepts
Tonga Institute of Higher Education IT 141: Information Systems
Automated test.
Unit Testing for the Absolute Beginner
Tonga Institute of Higher Education IT 141: Information Systems
Testing Strategies Sources: Code Complete, 2nd Ed., Steve McConnell
Automated test.
Junit Tests.
Intro to Programming (in JavaScript)
Principles of Object Oriented Programming
Presentation transcript:

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

Different kinds of tests Testing types Testing levels 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 http://en.wikipedia.org/wiki/Software_testing#Testing_Types 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 verify the system Source http://en.wikipedia.org/wiki/Software_te sting#Testing_levels Unit testing C# classes

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. Trying … to see if you can make it work Testing … to see if you can break it Unit testing C# classes

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# http://junit.org/ http://en.wikipedia.org/wiki/JUnit 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# classes

Unit testing C# classes 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# classes

Unit testing C# classes Assert A [TestMethod] should contain a number of calls to 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, 0.0001); 0.0001 is a so-called Delta: How close do expected and actual have to be Assert.IsTrue(teacher.Salary > 10000.00); 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 http://msdn.microsoft.com/en- us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx Unit testing C# classes

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(…) Figure credits https://kevin3stone.wordpress.com/tag/data-driven- testing/ [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# classes

How to organize a test: “Arrange, Act, Assert” pattern 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) http://xp123.com/articles/3a-arrange-act-assert/ Unit testing C# classes

Unit testing C# classes 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# classes

Unit testing C# classes Testing exceptions If a method might throw an exception, you must test 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 Using the annotation [ExpectedException(typeof(SomExceptionType))] Requires fewer programming lines Only a single exception can be tested in one test case 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# classes

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# classes

Help from Visual Studio Visual Studio can assist you in writing unit tests. You make an ordinary project for your ordinary classes. Make a unit test project Automatically Right click the class name and chose “Create Unit Tests” Class must be public + have at least one public method Public properties are not enough Manually 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# classes

Unit testing C# classes 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# classes

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# classes

Test-driven development (TDD) For each new feature in the program: Write (failing) test case Run the test, to see that it fails Write code until the test pass Refactor the code to acceptable standards Source http://en.wikipedia.org/wiki/Test- driven_development Unit testing C# classes

References and further readings Beck & Gamma JUnit Cookbook, http://junit.sourceforge.net/doc/cookbook/cookbook.htm Kent Beck & Erich Gamma invented Junit (the original unit testing framework) Microsoft Developer Networks Verifying Code by Using Unit Tests http://msdn.microsoft.com/en-us/library/dd264975(v=vs.110).aspx MSDN Unit Testing and Generating Source Code for Unit Test … Lists and groups Assert methods http://msdn.microsoft.com/en-US/library/ms364064(v=vs.80).aspx#utfwvs05tmsys_topic3 MSDN TestInitializeAttribute Class Includes an example using the MSTest annotation http://msdn.microsoft.com/en- us/library/microsoft.visualstudio.testtools.unittesting.testinitializeattribute.aspx Bill (William) Wake Arrange, Act, Assert (2011) http://xp123.com/articles/3a-arrange-act-assert/ Extreme Programming, Code the Unit Test First http://www.extremeprogramming.org/rules/testfirst.html Testing is an important discipline in XP (eXtreme Programming), which is another Kent Bech invention Unit testing C# classes