S
S
Sland Show2018-11-03 18:31:10
C++ / C#
Sland Show, 2018-11-03 18:31:10

How to initialize a checkable object for google tests?

I set up a project in CLion, connected a module from Google for testing.
And I don't quite understand how can I test objects?
For example, I have a matrix
I call:

#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

int main(int argc, char* argv[]) {
    // Running Unit-tests
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

And I'm trying to test:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "Matrix.h"

using namespace std;

// A new one of these is created for each test
class VectorTest : public testing::Test
{
public:
    Matrix userMatrix;
    Matrix testMatrix;

    virtual void SetUp() {
        int row;
        int col;

        std::cout << "Enter matrix size (col x row)\n";

        std::cout << "Row:"; std::cin >> row;
        std::cout << "Col:"; std::cin >> col;

        std::cout << "User enter matrix ( " << col << " x " << row << " )\n";

        // Create stack object instance
        userMatrix(col, row);

        std::cout << "Now, let's init matrix:...\n";

        // User input matrix instance
        std::cin >> userMatrix;

        std::cout << "After initialization:\n";
        std::cout << userMatrix;

        testMatrix(col, row);
        std::cout << "Let's init tester matrix\n";
        std::cin >> testMatrix;
        std::cout << "Result of tested matrix initialization \n" << testMatrix;
    }

    virtual void TearDown()
    {
    }
};

But nothing happens here and writes that 0 tests have been passed (I don’t even call cout). How do I properly test objects? Use mocks?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2018-11-03
@SlandShow

When you want to test an object, you write a test for it.

TEST( TestStaticString, GetChar )
{
  using TestString = Black::StaticString<'A', 'B', 'C'>;

  EXPECT_EQ( 'A', TestString::GetChar( 0 ) );
  EXPECT_EQ( 'B', TestString::GetChar( 1 ) );
  EXPECT_EQ( 'C', TestString::GetChar( 2 ) );
}

A word TESTis a macro that expands into a special binding for your test. Its first parameter is the name of the test case, the second is the name of the test.
You do the initialization of the object under test yourself.
Test checks are made using macros EXPECT_*and ASSERT_*.
Expect doesn't stop the test if it fails, but assert does. Thus, it is possible to divide the checks in the test into critical ones (there is no need to continue the test after such an error) and isolated ones (the test can be continued and get a lot of isolated errors).
For details, you can refer to an example from the Googletests themselves.
Your class VectorTestis a test fixture. You can apply it to your tests using the macro TEST_F. Read about ityou can here .
Fixtures are needed when for a group of your tests, start and stop are the same. In this case, the initialization and finalization of the object under test should be done in a fixture.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question