\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{blakecromar} \pdfinfo{ /Title (c-gtest.pdf) /Creator (Cheatography) /Author (blakecromar) /Subject (C++ (Gtest) Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{41A358} \definecolor{LightBackground}{HTML}{F3F9F4} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{C++ (Gtest) Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{blakecromar} via \textcolor{DarkBackground}{\uline{cheatography.com/218070/cs/48237/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}blakecromar \\ \uline{cheatography.com/blakecromar} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 30th June, 2026.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{::testing::Test — GTest Fixture}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class my\_fixture : public ::testing::Test \{ \newline protected: \newline // shared member variables \newline int shared\_variable; \newline \newline void SetUp() override \{ \newline shared\_variable = 42; // runs before each test \newline \} \newline \newline void TearDown() override \{ \newline // cleanup after each test \newline \} \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Base class for Google Test fixtures, providing automatic setup and teardown for each test. \newline \newline Inherit from it to create a fixture that shares member variables and helper functions across multiple tests. \newline \newline SetUp() runs before each test, TearDown() runs after each test — both are automatically called by Google Test.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\seqsplit{::testing::UnitTest::current\_test\_info()}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{const auto* info = ::testing::UnitTest::GetInstance()-\textgreater{}current\_test\_info(); \newline \newline info-\textgreater{}name(); // returns the test name as a string \newline info-\textgreater{}test\_suite\_name(); // returns the test suite name \newline info-\textgreater{}result(); // returns the test result} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Returns a pointer to information about the currently running test. \newline \newline Contains details like the test name, test suite name, and whether the test has failed. \newline \newline Commonly used in SetUp() to get the test name for creating unique temporary files or directories.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{EXPECT\_TRUE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{EXPECT\_TRUE(condition); // marks failed but continues running \newline ASSERT\_TRUE(condition); // stops test immediately if false} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A Google Test macro that checks if a condition is true and marks the test as failed if it isn't. \newline \newline Unlike ASSERT\_TRUE, the test continues running after a failure so other checks can still be evaluated. \newline \newline Use when you want to collect multiple failures in one test run rather than stopping at the first one.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{EXPECT\_LT}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{EXPECT\_LT(actual\_value, upper\_bound); \newline \newline // common usage \newline EXPECT\_LT(elapsed\_time, \seqsplit{std::chrono::seconds(5));} // verify operation was fast \newline EXPECT\_LT(count, 10); // verify count is under 10} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A Google Test macro that checks if the first value is less than the second, marking the test as failed if it isn't. \newline Unlike ASSERT\_LT, the test continues running after a failure so other checks can still be evaluated. \newline Commonly used to verify something happened quickly or within a time limit.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{::testing::Test::SetUp()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{void SetUp() override \{ \newline temp\_directory = create\_temp\_dir(); \newline \seqsplit{prepend\_to\_path(fake\_bin\_dir);} \newline saved\_path = get\_current\_path(); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A virtual function inherited from ::testing::Test that runs automatically before each individual test. \newline \newline Override it to initialise member variables, create temporary files, or prepare any state needed by the tests. \newline \newline Always paired with TearDown() to clean up anything created here.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\seqsplit{::testing::UnitTest::GetInstance()}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{const auto{\emph{ unit\_test = \seqsplit{::testing::UnitTest::GetInstance();} \newline const auto}} test\_info = unit\_test-\textgreater{}current\_test\_info(); \newline \newline // or chained together \newline const auto* test\_info = ::testing::UnitTest::GetInstance()-\textgreater{}current\_test\_info();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Returns a pointer to the single global instance of the UnitTest object. \newline \newline Uses the Singleton pattern — only one instance ever exists, and this method is how you access it. \newline \newline From it you can retrieve information about the currently running test suite and individual tests.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{ASSERT\_TRUE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{ASSERT\_TRUE(condition); // stops test immediately if false \newline EXPECT\_TRUE(condition); // marks failed but continues running} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A Google Test macro that checks if a condition is true and immediately aborts the test if it isn't. \newline \newline Use when the rest of the test cannot meaningfully continue if this check fails. \newline \newline Contrast with EXPECT\_TRUE which marks the test as failed but allows it to keep running.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{EXPECT\_EQ}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{EXPECT\_EQ(actual\_value, expected\_value);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A Google Test macro that checks if two values are equal and marks the test as failed if they aren't. \newline \newline Unlike ASSERT\_EQ, the test continues running after a failure so other checks can still be evaluated. \newline \newline Prints both the expected and actual values in the failure message making it easy to diagnose.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\seqsplit{testing::Test::TearDown()}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{void TearDown() override \{ \newline \seqsplit{restore\_path(saved\_path);} // restore original PATH \newline \seqsplit{remove\_temp\_directory(tmpDir);} // delete temporary files \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A virtual function inherited from ::testing::Test that runs automatically after each individual test. \newline \newline Override it to clean up anything created in SetUp() — delete temporary files, restore environment variables, free resources. \newline \newline Always runs even if the test fails, ensuring cleanup always happens.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{TEST\_F}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{TEST\_F(fixture\_class\_name}, test\_name) \newline \{ \newline // test body has full access to fixture members \newline // SetUp() has already run before this \newline // TearDown() will run automatically after this \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Defines a test that uses a fixture class, giving it access to the fixture's member variables and helper functions. \newline \newline Google Test automatically calls SetUp() before the test and TearDown() after, with a fresh fixture instance each time. \newline \newline The second argument is the test name, which must be unique within the fixture.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{EXPECT\_FALSE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{EXPECT\_FALSE(condition);} // marks failed but continues running \newline ASSERT\_FALSE(condition); // stops test immediately if true} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A Google Test macro that checks if a condition is false and marks the test as failed if it isn't. \newline \newline Unlike ASSERT\_FALSE, the test continues running after a failure so other checks can still be evaluated. \newline \newline Use when you want to verify something does not exist or did not happen.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{EXPECT\_NE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{EXPECT\_NE(actual\_value, other\_value); // fails if they are equal \newline \newline // common usage - string::find returns npos if not found \newline EXPECT\_NE(my\_string.find("target"), std::string::npos); // passes if "target" was found \newline EXPECT\_NE(result, 0); // passes if result is not zero} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A Google Test macro that passes when two values are not equal, and marks the test as failed if they are equal. \newline \newline Unlike ASSERT\_NE, the test continues running after a failure so other checks can still be evaluated. \newline \newline Commonly used to verify a search or find operation returned a valid result.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}