gtest | Mac OS X , Windows , Windows CE | Test Automation library
kandi X-RAY | gtest Summary
kandi X-RAY | gtest Summary
Google’s framework for writing C++ tests on a variety of platforms (Linux, Mac OS X, Windows, Windows CE, Symbian, etc). Based on the xUnit architecture. Supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal and non-fatal failures, various options for running the tests, and XML test report generation.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of gtest
gtest Key Features
gtest Examples and Code Snippets
Community Discussions
Trending Discussions on gtest
QUESTION
I am using google test(gtest/gmock) to add a couple of tests for the program(C++). For one of the class, I have to add a test to make sure the class is calling a function(let's say an important function I don't want to miss) in its constructor.
For example:
...ANSWER
Answered 2022-Mar-23 at 16:03Usually you test a class (which is not mocked) and mock the collaborators of that class (i.e. the classes that interact with it). The class being tested should not be mocked at the same time.
In your use case, however, it looks like you are trying to test a class AND mock the same class (or the parent class) at the same time.
I don't think you can do this with GMock specially if you want to test if something is called in the constructor because EXPECT_CALL
requires an object of the mocked class, and by the time you create that object, the constructor is called and finished. So, you need to make a clear distinction between the class-under-test and the collaborator classes that are mocked.
Interestingly if you try to do this here you will get a warning for an uninteresting call to bar, which is a sign that bar
was called, but it's not that useful because while you get the warning, your test will fail.
Instead, rather than using mocks and EXPECT_CALL
, I suggest you create and test a side effect of bar()
. To do this, you will have to slightly modify your class definitions. For example, you can write:
QUESTION
After failing to get coverage with-cmake I set up a minimalistic project to see if I can get coverage working that way. It's derived from using-gtest-without-cmake
It has a src
folder with a header and source file in it.
QuickMaths.hpp :
ANSWER
Answered 2022-Mar-23 at 10:57This error occurs because you are linking multiple files with the same name.
There are two clues to the problem:
When running the test, you will see a warning such as the following:
QUESTION
I am new to googletest/googlemock and I have following questions:
First question: I have this DatabaseClient class which has a method query_items which I want to mock. I`m unable to find the syntax to do it. Here is my attempt:
...ANSWER
Answered 2022-Mar-16 at 19:06Somehow you need to tell your TestService
class to use the mock object instead of the real object.
Currently you instantiate the DatabaseClient
in create()
:
QUESTION
Using the googletest framework I want to write my own main
function. Basically some custom initialization step needs to happen before RUN_ALL_TESTS
is called. I'd like to skip this step, if the command line parameters for googletest indicate, no tests should be run (e.g. if --gtest_list_tests
is passed).
Is it possible to retrieve this kind of information from the test framework without the need to parse the parameters myself?
What I'd like to accomplish:
...ANSWER
Answered 2022-Feb-15 at 23:03command line arguments are detected using the GTEST_FLAG
macro. An example of what you're trying to do might look like:
QUESTION
Issue
I need a help in fixing my unit test issue with gtest 1.10.0 version. When I tried to unit test involving a function that accepts std::experimental::any argument, exception is thrown and unit test terminated.
Steps to reproduce the issue
Snippet of unit tests covering my test scenario available under https://godbolt.org/z/Y7dvEsaPf In TestBoth testcase, if EXPECT_CALL and actual function calls are adjacently provided, exception is not thrown and test case execute successfully. But in my actual project code, my test function has call to send_data() function with both these data types.
Tool and operating system versions gtest version is 1.10.0 Ubuntu Linux 20.04
Compiler version
g++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0 C++14
Build system
cmake version 3.20.5
Additional context
Help needed or please direct to where I can get this query asked and get resolved.
...ANSWER
Answered 2022-Feb-15 at 02:53The issue is that AnyMatcher
matches successfully any std::any
. The solution is in forcing further expectations with help of ::testing::InSequence
:
QUESTION
I have a module written in C. In which I have API defined as follow.
...ANSWER
Answered 2022-Feb-12 at 03:17If you are using gcc
/g++
or clang
/clang++
you can use the linker option --wrap=symbol
to redirect calls to your own versions of these functions.
Your wrapper functions will have the name __wrap_symbol
and the real functions will be named __real_symbol
.
Example:
QUESTION
new keyword in C++ will throw an exception if insufficient memory but below code trying to return "NO_MEMORY" when new failed. This is bad because it will raise std::bad_alloc exception .
I am writing a unit test(gtest). How to create a scenario to catch this problem.
...ANSWER
Answered 2022-Jan-31 at 14:36First I think you need to catch the exception otherwise your program will never reach the point of returning NO_MEMORY
:
QUESTION
I have a test suite setup that takes some time to run. (Open a serial port initialize communication with a board, etc).
I would like to run a test in a loop, but without running this setups and cleanups every time because they are not needed, and only consume time.
The default behavior of TestSuiteSetup is to be executed once and then allow any number of tests from that suite to run. Running multiple tests in a suite or repeating one test are actually the same use case, but it seems not to be supported by -repeat. (I would expect it to be possible to combine the flag with an option like: -run_setup_only_once)
Is this possible in gtest? Or is there another way to achieve this?
...ANSWER
Answered 2022-Jan-18 at 00:42There are several ways to achieve this.
- Implementing your own
main
, exclude gtest_main.cc from your project:
QUESTION
I want to defer the execution of a packaged task in a loop.
...ANSWER
Answered 2021-Dec-29 at 13:52By default a lambda's call operator is const
-qualified.
Inside the lambda's body the this
pointer to the lambda is therefore also const
-qualified and so is the member wrapper
.
std::packaged_task
does not have a const
-qualified operator()
, so it cannot be called.
You can make the lambda's operator()
non-const
-qualified by adding the mutable
keyword:
QUESTION
My main question is about a specific gmock error but to provide some background. I have been working on a larger project where I have different implementations of the same interface. I want to be able to instantiate a class using the interface and choose which interface implementation it has to use (when creating the class instance). The interface is not shared and each instance of the interface is only used by one class instance. In order to do this I use dependency injection and I store the interface instance in a unique_ptr. This works properly and gives me the desired behavior. To unit test some the class I used gmock to mock the interface and then injected the mocked interface into my class under test. To my surprise gmock informed me that I had a memory leak which I do not understand. A simplified piece of code produces the same error:
...ANSWER
Answered 2021-Dec-29 at 17:58std::unique_ptr base = std::move(mock);
is where the memory leak happens: *base
will be destroyed as a Base
, not a Mock
, when base
is destroyed.
The best fix is to add a virtual destructor (virtual ~Base() = default;
), which you should have for a struct that has any other virtual members.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install gtest
Suppose you put Google Test in directory ${GTEST_DIR}. To build it, create a library build target (or a project as called by Visual Studio and Xcode) to compile.
Before settling on CMake, we have been providing hand-maintained build projects/scripts for Visual Studio, Xcode, and Autotools. While we continue to provide them for convenience, they are not actively maintained any more. We highly recommend that you follow the instructions in the previous two sections to integrate Google Test with your existing build system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page