breadcrumbs: Blink (Rendering Engine) > page_name: unittesting title: Unit Testing in Blink
WARNING: This document is a work in progress!
GTest and GMock are both imported into Blink and can be used in unit tests. Most existing tests are purely GTest based, but GMock is slowly being used more.
“Google's framework for writing C++ tests on a variety of platforms (Linux, Mac OS X, Windows, Cygwin, Windows CE, and Symbian). Based on the xUnit architecture. Supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal and non-fatal failures, value- and type-parameterized tests, various options for running the tests, and XML test report generation.”
If you are using gtest “Death Tests” or GMock's EXPECT_THAT with MatchesRegex or ContainsRegex, you have to be very careful with your regex.
There is no common syntax between all operating system for character class matches;
EXPECT_THAT("abc", MatchesRegex("\w*")) # Does NOT work on Mac OS X.
EXPECT_THAT("abc", MatchesRegex("[a-c]*")) # Does NOT work on Windows.
(CL https://codereview.chromium.org/55983002/ proposes making chromium only use the gtest internal regex engine which would fix this problem.)
You can use GMock EXPECT_THAT and the GMock matchers inside a GTest test for more powerful matching.
Quick example;
EXPECT_THAT(Foo(), testing::StartsWith("Hello"));EXPECT_THAT(Bar(), testing::MatchesRegex("Line \\d+"));ASSERT_THAT(Baz(), testing::AllOf(testing::Ge(5), testing::Le(10)));Value of: Foo() Actual: "Hi, world!"Expected: starts with "Hello"
More information at;
More information at https://code.google.com/p/googletest/issues/detail?id=442
Workaround:
https://code.google.com/p/googlemock/
Inspired by jMock, EasyMock, and Hamcrest, and designed with C++'s specifics in mind, Google C++ Mocking Framework (or GMock for short) is a library for writing and using C++ mock classes.
GMock uses the gtest for doing the regexs, see the section under gtest above.
For speed reasons, a majority of Blink's functions are non-virtual. This can make them quite hard to mock. Here are some tips for working around these problems;
Useful documentation:
Using a proxy interface internally in your class;
OwnPtr and mocking objects can be tricky to get right, here is some important information.
The Problem
As normal, once you return an object via a PassOwnPtr you no longer control the life cycle of the object. This means that you must not use the object as an expectation (EXPECT_CALL) for another function call because;
Here is some example code which is WRONG:
Solutions that don't work
Creating a specialization of OwnedPtrDeleter
template <> struct OwnedPtrDeleter<MyClass> {}
Why?
The OwnedPtrDeleter specialization must be visible at the location that the OwnPtr/PassOwnPtr is created.
Test helpers are an important part of making Blink easier to test for everyone. The more test helpers that exist, the easier it is to write new unit tests as you have to write less boilerplate code and find it easier to debug failing tests.
Test helpers include;
operator==
definitions to allow EXPECT_EQ
and comparisons in EXPECT_CALL
mocks to work.Test helpers should;
EXPECT_THAT
with Regex from GMock to make these tests easier to write.Both the EXPECT_EQ
and the EXPECT_CALL
methods use a == b
to compare if two objects are equal. However for many reasons you don't want this operator to be generally available in Blink. You can define the operator in the test helper instead and then it will only be available during tests.
Unlike PrintTo, operator== is not a template so the normal type hierarchy applies.
The operator== MUST be define in the same namespace as the type for EXPECT_EQ
to work. For example, if type is ::WebCore::AnimatableValue
the operator must be in the ::WebCore
namespace.
Pretty printers make it much easier to see what is going on in your test and why a match isn't working. They should be created for any simple type which has a useful string representation.
void PrintTo(const A& a, ::std::ostream* os) { *os << "A@" << &a; }
More Information on creating pretty printers can be found at GTest Advanced Guide: Teaching Google Test How to Print Your Values.
Pretty Printers must be define in the same namespace as the class which it is printing.
Pretty Printers only work on exact and known type match. This means that;
Further information at the gtest bug tracker - https://code.google.com/p/googletest/issues/detail?id=443
This is hard to understand, but shown by the following example (also attached as printto-confusing.cpp).
You can work around this problem by also defining a couple of extra PrintTo methods (also attached as printto-workaround.cpp).
All these issues are up for discussion and have yet to be decided on;