Exploring Unit test and Google test
Published: 2024-02-26 05:43:37

Hi Readers, I am Nithyanand, working as Junior software engineer at Luxoft. In this article, I have tried to give a Introduction to Unit Testing and Google Testing Framework.

Testing:

Testing is the process of verifying the software performs to the specifications.
The developer will execute the program with selected subset of all the possible input, and compares the actual output with the expected output.
Benefits of testing include

Preventing bugs

Reducing development costs and improving performance so on..

What is Unit Testing?

Unit testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected. Unit Testing is done during the development (coding phase) of an application by the developers. Unit Tests isolate a section of code and verify its correctness. A unit may be an individual function, method, procedure, module, or object.
In SDLC, STLC, V Model, Unit testing is first level of testing done before integration testing. Unit testing is a White Box testing technique that is usually performed by the developer.

Why unit testing?

Unit Testing is important because software developers sometimes try saving time doing minimal unit testing and this is myth because inappropriate unit testing leads to high cost Defect fixing during System Testing, Integration Testing and even Beta Testing after application is built. If proper unit testing is done in early development, then it saves time and money in the end.

Obviously tests improve the quality of your code, it helps the developers to understand the testing code base and enables them to make changes quickly.

Unit tests help with code re-use.

Give developers confidence when adding behavior or making fixes.

How to do Unit Testing?

In order to do Unit Testing, developers write a section of code to test a specific function in software application. Developers can also isolate this function to test more rigorously which reveals unnecessary dependencies between function being tested and other units so the dependencies can be eliminated. Developers generally use Unit Test framework to develop automated test cases for unit testing.
The Unit Testing simply stated, they are software tools to support writing and running unit tests, including a foundation on which to build tests and the functionality to execute the tests and report their results.

What is Google testing?

It is a test framework, The google test is a library for writing the unit test for C++ or Google test is a one type of software tool for writing as well as running the unit test.

In google testing the code is tested line-by-line.

It is based on xUnit architecture which is a set of Frameworks for programming and automated execution of test cases.

Why Google testing?

Google Test helps us to write better C++ tests.

Independent and Repeatable: Google test isolates the tests by running each of them on a different object.

Portable and Reusable: Google test works on different Oses (Linux, Windows, or a Mac), with different compilers.

When tests fail, it will provide much information about the problem, so it will be easy to solve the issues.

Basic concepts and Assertions in Google test :
Assertions are statements that check whether a condition is true. An assertion's result can be

Success
2.Nonfatal failure, or fatal failure

Note :- If the fatal failure occurs, it will abort the current function else the program continues normally.

Assertions in tests used to verify the behavior of the tested code. If a test has a failed assertion, then it fails else it succeeds.

These assertions do basic true/false condition testing.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjfu9fc0cancz4rqjan4.png)



List of rational operator for Google testing :


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pv81hd2k1whrxvivw5go.png)



Simple tests

To write test cases:

The test file shoulde be having the #include "gtest/gtest.h"

Use the macro TEST_F() for defining and naming a test function, These are ordinary C++ functions that don't return a value.

We can use various Google Test assertions to check values of the tests.

Test results will be based on assertions, if any assertions in the test fails Then the entire test fails else it succeeds.

Note :- Can write many assertions in single test case.

Example:-

include "gtest/gtest.h"
class SumTest: public ::testing::Test
{
public:
SumTest() {};
virtual ~SumTest() {};
/// SetUp will be called before each test that uses this fixture.
virtual void SetUp() {};
/// Will be called after each test if the test uses this fixture.
virtual void TearDown() {};
};

// Test of sum.
TEST_F(SumTest, Test_T1) {
EXPECT_EQ(2, sum(1 + 1));
EXPECT_LT(3, sum(2 + 2));
EXPECT_GT(4, sum(3 + 0);
EXPECT_NQ(5, sum(8 + 1);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Google test environment:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/988eozx66o1vdyr7v0ha.png)



Actual testcases:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2wb2urd4eylj0ynxdgi.png)




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8wlee9538z846cipx02.png)



Conclusion:

In the article i explained basics of the unit testing and google testing. Getting started with Google Test is pretty easy.It has more features than other tests.Google test can be used to prevent the bugs at the intial stage, which will reduce the more debuging time and cost in the end.
This article just scratches the surface of the Google C++ Testing Framework. Detailed documentation about the framework is available from the Google site.