Download presentation
Presentation is loading. Please wait.
Published byIlene Valerie Reynolds Modified over 9 years ago
1
Introduction to Unit Testing
2
Unit testing – testing methods/classes of the program Test suite is composed of several tests Test suite should cover all the statements Tests calls Assert functions
3
What is the Assert Class One Assert per test – Short circuit – Write simple and concise tests Checks if different conditions are true
4
Assert Functions Assert::IsFalse(…) - 3 Basic Overloads -Assert::IsFalse( bool condition); -Assert::IsFalse( bool condition, string message ); -Assert::IsFalse( bool condition, string message, object[] parms ); One Assert per test – Short circuit – Simple and concise tests
5
Assert Functions AreEqual(int actual, int expected) AreEqual(double actual, double expected) AreEqual(float actual, float expected) AreEqual(decimal actual, decimal expected) AreEqual(object actual, object expected) AreSame (object actual, object expected)
6
Assert Functions IsTrue( bool condition) IsFalse( bool condition) IsNull( object obj) IsNotNull( object obj) Fail() Ignore()
7
Tools available NUnit WinUnit Visual Studio 2005 Team Edition Visual Studio 2008 Professional Edition
8
Exercise – Use Unit testing in Visual Studio 2008
9
Create a new test project Create a new solution and project Add name Click OK
10
Add subject class Download from blackboard the files: – Grade.cpp – Grade.h Save and add the files in your project Some configuration properties need to be changed – See next 2 slides
11
Configuring…Project->Properties (Alt+F7)
12
Configuring – more…
13
Configuring…Done! Build the project Run the test cases – Test->Run->All Tests in Solution
14
Writing tests Write a test for setScore(double) method void setScore(double newscore){ if ((0<=newscore)&&(newscore<=100)){ score = newscore; } What if newscore = 100 [TestMethod] void TestMethod1() { Grade *g = new Grade(); g->setScore(100); Assert::AreEqual(100.0, g->getScore()); }; What if newscore = -1 What if newscore = 101
15
Exercises Complete the exercises from the file: Unit Testing Tutorial.doc
16
Warning Sometimes configuring larger projects to include testing is not trivial – Testing in Notepad++ and WinMerge - not yet Other unit testing tools are easier to configure – JUnit – testing Java programs – They follow the same principles
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.