1 / 16

Introduction to Unit Testing

Introduction to Unit Testing. 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. What is the Assert Class. One Assert per test Short circuit Write simple and concise tests

eamon
Download Presentation

Introduction to Unit Testing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

More Related