<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Information &#8211; Naseef Chowdhury</title>
	<atom:link href="https://naseefchowdhury.me/tag/information/feed/" rel="self" type="application/rss+xml" />
	<link>https://naseefchowdhury.me</link>
	<description></description>
	<lastBuildDate>Fri, 28 Jun 2024 13:01:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://naseefchowdhury.me/wp-content/uploads/2019/04/cropped-003-ads-32x32.png</url>
	<title>Information &#8211; Naseef Chowdhury</title>
	<link>https://naseefchowdhury.me</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>A Comprehensive Guide to GTest Automation Testing (Naseef Chowdhury)</title>
		<link>https://naseefchowdhury.me/a-comprehensive-guide-to-gtest-automation/</link>
					<comments>https://naseefchowdhury.me/a-comprehensive-guide-to-gtest-automation/#respond</comments>
		
		<dc:creator><![CDATA[Naseef Chowdhury]]></dc:creator>
		<pubDate>Fri, 28 Jun 2024 12:55:26 +0000</pubDate>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[IT and Technology]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Google Testing]]></category>
		<category><![CDATA[Google updates]]></category>
		<category><![CDATA[Gtest]]></category>
		<guid isPermaLink="false">https://naseefchowdhury.me/?p=3346</guid>

					<description><![CDATA[In the rapidly evolving world of software development, ensuring the reliability and robustness of code through rigorous testing is paramount. One of the most widely adopted frameworks for C++ unit testing is Google Test, commonly called GTest. This article will provide an in-depth look at GTest, its features, and how to implement automation testing using [&#8230;]]]></description>
										<content:encoded><![CDATA[<p id="2fca" class="pw-post-body-paragraph vu vv rk vw b vx vy vz wa wb wc wd we wf wg wh wi wj wk wl wm wn wo wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">In the rapidly evolving world of software development, ensuring the reliability and robustness of code through rigorous testing is paramount. One of the most widely adopted frameworks for C++ unit testing is Google Test, commonly called GTest. This article will provide an <a href="https://en.wikipedia.org/wiki/Google_Test" target="_blank" rel="noopener">in-depth look at GTest</a>, its features, and how to implement automation testing using this powerful framework effectively.</p>
<h1 id="a987" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;" data-selectable-paragraph="">Introduction to GTest</h1>
<p id="f0c8" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Google Test (GTest) is an open-source <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://medium.com/@naseefcse/modern-c-features-demystified-a-guide-for-software-engineers-bfa05b56ba5e" target="_blank" rel="noopener">testing framework for C++</a></span>. Developed by Google, it provides a rich set of features for writing and running tests, making it an ideal choice for simple and complex testing scenarios. GTest supports various testing paradigms, including unit tests, integration tests, and even system tests, offering a flexible and comprehensive testing solution.</p>
<h1 id="4b60" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;" data-selectable-paragraph="">Key Features of GTest</h1>
<h2 id="b407" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">1. Rich Assertion Library</h2>
<p id="c446" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">GTest comes with an extensive set of assertions that enable detailed verification of test results. These assertions range from simple checks like <code class="fk xg xh xi xj b">EXPECT_EQ</code> for comparing values to more complex ones like <code class="fk xg xh xi xj b">ASSERT_THROW</code> for verifying exception handling.</p>
<h2 id="b463" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">2. Test Fixtures</h2>
<p id="8836" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Test fixtures in GTest allow the setup of common test data and the teardown of resources after tests run. This feature is particularly useful for testing classes or functions that require specific states or configurations.</p>
<h2 id="e102" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">3. Parameterization</h2>
<p id="66ce" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">GTest supports parameterized tests, which allow the same test logic to be executed with different data sets. This feature enhances test coverage without duplicating test code.</p>
<h2 id="b88f" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">4. Mocking Support</h2>
<p id="6e7b" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Through Google Mock, GTest provides robust mocking capabilities, allowing the creation of mock objects and the specification of expected behaviors. This is crucial for isolating the unit under test and simulating various conditions and <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://medium.com/@naseefcse/unlocking-real-time-communication-a-guide-to-basic-pubsub-module-in-c-1428a8a7cf83" target="_blank" rel="noopener">interactions</a></span>.</p>
<h1 id="0d0b" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;" data-selectable-paragraph="">Setting Up GTest</h1>
<p id="2d8e" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Before diving into automation, setting up GTest in your development environment is essential. Here’s a step-by-step guide:</p>
<h3 id="9dbb" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">1. Installation</h3>
<p id="143c" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">You can install GTest using package managers or by building it from a source. For instance, with <em class="xk">vcpkg</em>:</p>
<table style="height: 51px; width: 71.3513%; border-collapse: collapse; background-color: #d6fff7;">
<tbody>
<tr style="height: 51px;">
<td style="width: 100%; height: 51px;"><em><strong>                                           vcpkg install gtest</strong></em></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">Or, by adding GTest to your CMake project:</p>
<p style="text-align: justify;">
<table style="width: 100%; border-collapse: collapse; background-color: #d6fff7;">
<tbody>
<tr>
<td style="width: 100%;"><span class="hljs-built_in">cmake_minimum_required</span>(VERSION <span class="hljs-number">3.10</span>)<br />
<span class="hljs-built_in">project</span>(MyProject)# Add GoogleTest directly to your project.<br />
<span class="hljs-built_in">add_subdirectory</span>(${CMAKE_SOURCE_DIR}/path/to/googletest)<span class="hljs-built_in">enable_testing</span>()# <span class="hljs-function">Link your test executable against gtest <span class="hljs-keyword">and</span> gtest_main<br />
<span class="hljs-title">add_executable</span><span class="hljs-params">(MyTest test.cpp)</span><br />
<span class="hljs-title">target_link_libraries</span><span class="hljs-params">(MyTest gtest gtest_main)</span></span></td>
</tr>
</tbody>
</table>
<h3 style="text-align: justify;"></h3>
<h3 id="0ed7" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;"></h3>
<h3 class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">2. Writing Tests</h3>
<p id="ffcf" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Here’s a simple example of a GTest unit test:</p>
<table style="width: 100%; border-collapse: collapse; background-color: #d6fff7;">
<tbody>
<tr>
<td style="width: 100%;"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;gtest/gtest.h&gt;</span></span></p>
<p><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">Add</span><span class="hljs-params">(<span class="hljs-type">int</span> a, <span class="hljs-type">int</span> b)</span> </span>{<br />
<span class="hljs-keyword">return</span> a + b;<br />
}</p>
<p><span class="hljs-built_in">TEST</span>(AdditionTest, PositiveNumbers) {<br />
<span class="hljs-built_in">EXPECT_EQ</span>(<span class="hljs-built_in">Add</span>(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>), <span class="hljs-number">5</span>);<br />
}</p>
<p><span class="hljs-built_in">TEST</span>(AdditionTest, NegativeNumbers) {<br />
<span class="hljs-built_in">EXPECT_EQ</span>(<span class="hljs-built_in">Add</span>(<span class="hljs-number">-2</span>, <span class="hljs-number">-3</span>), <span class="hljs-number">-5</span>);<br />
}</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;"><img fetchpriority="high" decoding="async" class="aligncenter wp-image-3354 " src="https://naseefchowdhury.me/wp-content/uploads/2024/06/Untitled-design-1-1024x683.png" alt="Guide to GTest Automation Testing" width="505" height="336" /></p>
<h3 id="11db" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">3. Running Tests</h3>
<p id="ade0" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">To run the tests, execute the test binary. GTest will automatically detect and run all defined tests:</p>
<table style="height: 45px; width: 74.0541%; border-collapse: collapse; background-color: #d6fff7;">
<tbody>
<tr style="height: 45px;">
<td style="width: 100%; height: 45px;">                                                         ./MyTest</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;"><img decoding="async" class="aligncenter" src="https://miro.medium.com/v2/resize:fit:1023/1*3pMcwzdh5yq8yanTkdMEOg.jpeg" width="595" height="442" /></p>
<p style="text-align: justify;">
<h1 id="157d" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;" data-selectable-paragraph="">Automating GTest</h1>
<p id="7364" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph=""><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://medium.com/@naseefcse" target="_blank" rel="noopener">Automation</a></span> is key to integrating GTest into continuous integration (CI) pipelines and ensuring tests are consistently executed. Here’s how you can automate GTest:</p>
<h3 id="a2dc" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">1. Integrating with CI Tools</h3>
<p id="553d" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Most CI tools, such as Jenkins, Travis CI, and GitHub Actions, support running GTest. Here’s an example of a GitHub Actions workflow:</p>
<table style="width: 100%; border-collapse: collapse; background-color: #d6fff7;">
<tbody>
<tr>
<td style="width: 100%;">name: CI</p>
<p>on: [push, pull_request]</p>
<p><span class="hljs-built_in">jobs</span>:<br />
build:<br />
runs-on: ubuntu-latest</p>
<p>steps:<br />
&#8211; uses: actions/checkout@v2<br />
&#8211; name: Set up CMake<br />
uses: actions/setup-cmake@v1<br />
&#8211; name: Configure<br />
run: cmake . -Bbuild<br />
&#8211; name: Build<br />
run: cmake &#8211;build build<br />
&#8211; name: Run Tests<br />
run: ./build/MyTest</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p style="text-align: justify;">
<h3 id="1abe" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">2. Generating Test Reports</h3>
<p id="def9" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Generating and publishing test reports is crucial for monitoring and maintaining code quality. GTest supports various output formats, including XML. You can configure GTest to generate XML reports as follows:</p>
<table style="height: 50px; width: 100%; border-collapse: collapse; background-color: #d6fff7;">
<tbody>
<tr style="height: 50px;">
<td style="width: 100%; height: 50px;">./MyTest &#8211;gtest_output=xml:report.xml</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
<p id="4669" class="pw-post-body-paragraph vu vv rk vw b vx vy vz wa wb wc wd we wf wg wh wi wj wk wl wm wn wo wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">These reports can be integrated into CI pipelines to provide insights into test results and trends over time.</p>
<h3 id="7de2" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">3. Advanced Automation Techniques</h3>
<p id="dae3" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">For more advanced automation, consider the following techniques:</p>
<ul class="" style="text-align: justify;">
<li id="9960" class="vu vv rk vw b vx vy vz wa wb wc wd we wf wg wh wi wj wk wl wm wn wo wp wq wr yh yi yj bj" data-selectable-paragraph=""><strong class="vw me">Test Coverage Analysis</strong>: Use tools like gcov or lcov to analyze code coverage and identify untested code paths.</li>
<li id="5e7f" class="vu vv rk vw b vx yk vz wa wb yl wd we wf ym wh wi wj yn wl wm wn yo wp wq wr yh yi yj bj" data-selectable-paragraph=""><strong class="vw me">Parallel Test Execution</strong>: Speed up test execution by running tests in parallel using tools like CTest or Ninja.</li>
<li id="21d6" class="vu vv rk vw b vx yk vz wa wb yl wd we wf ym wh wi wj yn wl wm wn yo wp wq wr yh yi yj bj" data-selectable-paragraph=""><strong class="vw me">Automated Mocking</strong>: Integrate Google Mock for automated testing of complex interactions and dependencies.</li>
</ul>
<h2 id="7cfb" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;">Best Practices for GTest Automation</h2>
<p id="3d54" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">To maximize the benefits of GTest automation, adhere to these best practices:</p>
<ol class="" style="text-align: justify;">
<li id="60c0" class="vu vv rk vw b vx vy vz wa wb wc wd we wf wg wh wi wj wk wl wm wn wo wp wq wr yp yi yj bj" data-selectable-paragraph=""><strong class="vw me">Write Clear and Concise Tests</strong>: Ensure tests are easy to understand and maintain. Each test should verify a single behavior or scenario.</li>
<li id="eea3" class="vu vv rk vw b vx yk vz wa wb yl wd we wf ym wh wi wj yn wl wm wn yo wp wq wr yp yi yj bj" data-selectable-paragraph=""><strong class="vw me">Use Test Fixtures Wisely</strong>: Leverage test fixtures to manage common setup and teardown tasks, reducing code duplication and improving test organization.</li>
<li id="99d5" class="vu vv rk vw b vx yk vz wa wb yl wd we wf ym wh wi wj yn wl wm wn yo wp wq wr yp yi yj bj" data-selectable-paragraph=""><strong class="vw me">Parameterize Tests</strong>: Use parameterized tests to cover multiple input scenarios without duplicating test code.</li>
<li id="746f" class="vu vv rk vw b vx yk vz wa wb yl wd we wf ym wh wi wj yn wl wm wn yo wp wq wr yp yi yj bj" data-selectable-paragraph=""><strong class="vw me">Monitor Test Results</strong>: Regularly review test reports and address failing tests promptly to maintain code quality and reliability.</li>
<li id="b52e" class="vu vv rk vw b vx yk vz wa wb yl wd we wf ym wh wi wj yn wl wm wn yo wp wq wr yp yi yj bj" data-selectable-paragraph=""><strong class="vw me">Integrate with CI/CD</strong>: Ensure GTest is integrated into your CI/CD pipeline for continuous testing and feedback.</li>
</ol>
<h1 id="09a7" class="ws wt rk be wu mf wv mg mi mj ww mk mm ix wx iy jb mp wy mq mt mu wz mv my xa bj" style="text-align: justify;" data-selectable-paragraph="">Conclusion</h1>
<p id="d558" class="pw-post-body-paragraph vu vv rk vw b vx xb vz wa wb xc wd we wf xd wh wi wj xe wl wm wn xf wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">GTest is a powerful and flexible framework for automating unit <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://medium.com/@naseefcse/unlocking-real-time-communication-a-guide-to-basic-pubsub-module-in-c-1428a8a7cf83" target="_blank" rel="noopener">tests in C++ projects.</a></span> By leveraging its rich features and integrating it into CI/CD pipelines, developers can enhance code quality, increase efficiency, and ensure robust software delivery. Following best practices and continuously monitoring test results will help maintain a high standard of code reliability and performance.</p>
<p id="c144" class="pw-post-body-paragraph vu vv rk vw b vx vy vz wa wb wc wd we wf wg wh wi wj wk wl wm wn wo wp wq wr fx bj" style="text-align: justify;" data-selectable-paragraph="">Embrace the power of GTest and automation to transform your testing process and achieve greater software excellence.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://naseefchowdhury.me/a-comprehensive-guide-to-gtest-automation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
