GoogleTest Primer. Outline Basic Concepts Assertions Basic Assertions Binary Comparison String Comparison Floating-Point Comparison Simple Tests Test.

Slides:



Advertisements
Similar presentations
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
VBA Modules, Functions, Variables, and Constants
Chapter 5: Loops and Files.
Program Input and the Software Design Process ROBERT REAVES.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Object Oriented Programming
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
CPS120 Introduction to Computer Science Iteration (Looping)
Google C++ Testing Framework Death Tests. In many applications, there are assertions that can cause application failure if a condition is not met. ◦ Square.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
CPS120: Introduction to Computer Science Decision Making in Programs.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Computer Engineering Rabie A. Ramadan Lecture 5.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CSCI-383 Object-Oriented Programming & Design Lecture 18.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CPS120 Introduction to Computer Science Iteration (Looping)
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Loops and Files. 5.1 The Increment and Decrement Operators.
Google C++ Testing Framework Test Fixtures: Using the Same Data Configuration for Multiple Tests.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Unit 6 Repetition Processing Instructor: Brent Presley.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Object Orientated Programming in Perl Simulated Models of Termites.
Google C++ Testing Framework Part 2: Assertion. Concepts A test case contains one or many tests. ◦ You should group your tests into test cases that reflect.
Learners Support Publications Constructors and Destructors.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lec 24 Googletest - 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 24 Google C++ Framework 11/20/
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Constructors and Destructors
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Why exception handling in C++?
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
This pointer, Dynamic memory allocation, Constructors and Destructor
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Conditions and Ifs BIS1523 – Lecture 8.
Constructors and Destructors
9-10 Classes: A Deeper Look.
C++ Constructor Insanity CSE 333 Summer 2018
Peter Seibel Practical Common Lisp Peter Seibel
9-10 Classes: A Deeper Look.
SPL – PS1 Introduction to C++.
SPL – PS3 C++ Classes.
Presentation transcript:

GoogleTest Primer

Outline Basic Concepts Assertions Basic Assertions Binary Comparison String Comparison Floating-Point Comparison Simple Tests Test Fixtures Invoking the Tests Writing the main() Function

Basic Concepts Start by writing assertions. An assertion's result can be success, nonfatal failure, or fatal failure Test Program Test Case Test

Assertions Assertions are macros that resemble function calls Test a class or function by making assertions about its behavior ASSERT_* generate fatal failures when they fail, and abort the current function EXPECT_* generate nonfatal failures, which don't abort the current function

Assertions Usually EXPECT_* are preferred, as they allow more than one failures to be reported in a test However, you should use ASSERT_* if it doesn't make sense to continue when the assertion in question fails

Basic Assertions ASSERT_* yields a fatal failure and returns from the current function, while EXPECT_* yields a nonfatal failure, allowing the function to continue running

Binary Comparison put the expression you want to test in the position of actual, and put its expected value in expected

Binary Comparison Value arguments must be comparable by the assertion's comparison operator Values must support the << operator for streaming to an ostream If the corresponding operator is defined for the user-defined type, prefer using the ASSERT_*() macros

Binary Comparison It's OK for the arguments to have side effects, because arguments are always evaluated exactly once But your code should not depend on any particular argument evaluation order ASSERT_EQ() does pointer equality on pointers. If used on two C strings, use ASSERT_STREQ() instead

String Comparison The assertions in this group compare two C strings. If you want to compare two string objects, use EXPECT_EQ, EXPECT_NE, and etc instead

String Comparison Note that "CASE" in an assertion name means that case is ignored A NULL pointer and an empty string are considered different.

Floating-Point Comparison Due to round-off errors, it is very unlikely that two floating-points will match exactly For floating-point comparison to make sense, the user needs to carefully choose the error bound Google Test provides a good default assertions to do the comparison in terms of Units in the Last Place (ULPs)

Floating-Point Comparison By "almost equal", we mean the two values are within 4 ULP's from each other The above assertion allow you to choose the acceptable error bound

Simple Tests 1.Use the TEST() macro to define and name a test function, These are ordinary C++ functions that don't return a value 2.In this function, along with any valid C++ statements you want to include, use the various Google Test assertions to check values 3.The test's result is determined by the assertions; if any assertion in the test fails (either fatally or non-fatally), or if the test crashes, the entire test fails. Otherwise, it succeeds

Simple Tests The first argument is the name of the test case, and the second argument is the test's name within the test case A test's full name consists of its containing test case and its individual name Tests from different test cases can have the same individual name

Simple Tests Google Test groups the test results by test cases, so logically-related tests should be in the same test case; in other words, the first argument to their TEST() should be the same

Test Fixtures A test fixture allows you to reuse the same configuration of objects for several different tests

Test Fixtures 1.Derive a class from testing::Test. Start its body with protected: or public: as we'll want to access fixture members from sub-classes 2.Inside the class, declare any objects you plan to use 3.If necessary, write a default constructor or SetUp() function to prepare the objects for each test 4.If necessary, write a destructor or TearDown() function to release any resources you allocated in SetUp() 5.If needed, define subroutines for your tests to share

Test Fixtures TEST_F() allows you to access objects and subroutines in the test fixture The first argument is the test case name, but for TEST_F() this must be the name of the test fixture class A test fixture class must be defined before using it in a TEST_F()

Test Fixtures For each test defined with TEST_F(), Google Test will: 1.Create a fresh test fixture at runtime 2.Immediately initialize it via SetUp(), 3.Run the test 4.Clean up by calling TearDown() 5.Delete the test fixture Google Test does not reuse the same test fixture for multiple tests. Any changes one test makes to the fixture do not affect other tests

Test Fixtures By convention, you should give it the name FooTest where Foo is the class being tested In this case, TearDown() is not needed since we don't have to clean up after each test, other than what's already done by the destructor

Test Fixtures When these tests run, the following happens: 1. Google Test constructs a QueueTest object (let's call it t1 ). 2. t1.SetUp() initializes t1. 3. The first test ( IsEmptyInitially ) runs on t1. 4. t1.TearDown() cleans up after the test finishes. 5. t1 is destructed. The above steps are repeated on another QueueTest object, this time running the DequeueWorks test When these tests run, the following happens: 1. Google Test constructs a QueueTest object (let's call it t1 ). 2. t1.SetUp() initializes t1. 3. The first test ( IsEmptyInitially ) runs on t1. 4. t1.TearDown() cleans up after the test finishes. 5. t1 is destructed. The above steps are repeated on another QueueTest object, this time running the DequeueWorks test

Test Fixtures The rule of thumb is to use EXPECT_* when you want the test to continue to reveal more errors after the assertion failure, and use ASSERT_* when continuing after failure doesn't make sense

Invoking the Tests After defining your tests, you can run them with RUN_ALL_TESTS(), which returns 0 if all the tests are successful, or 1 otherwise RUN_ALL_TESTS() runs all tests in your link unit -- they can be from different test cases, or even different source files

Invoking the Tests When invoked, the RUN_ALL_TESTS() macro: 1.Saves the state of all Google Test flags 2.Creates a test fixture object for the first test 3.Initializes it via SetUp() 4.Runs the test on the fixture object 5.Cleans up the fixture via TearDown() 6.Deletes the fixture 7.Restores the state of all Google Test flags 8.Repeats the above steps for the next test, until all tests have run

Invoking the Tests Whether a test has passed based on its exit code. Thus your main() function must return the value of RUN_ALL_TESTS() RUN_ALL_TESTS() should be called only once. Calling it more than once conflicts with some advanced Google Test features and thus is not supported

Writing the main() Function Include gtest/gtest.h to use googletest facilities

Writing the main() Function This function parses the command line for Google Test flags, and removes all recognized flags. This allows the user to control a test program's behavior via various flags, which we'll cover in GTestAdvanced. You must call this function before calling RUN_ALL_TESTS(), or the flags won't be properly initialized. In your main() function must return the value of RUN_ALL_TESTS()

Reference