From 4b6ee69937e05d64e887e0022c3b3093b88ea011 Mon Sep 17 00:00:00 2001 From: bialger <artur.bigulov@yandex.ru> Date: Mon, 22 Jan 2024 14:41:46 +0300 Subject: [PATCH] Created proper test structure --- tests/CMakeLists.txt | 5 +++++ tests/ProjectIntegrationTestSuite.cpp | 9 ++++++++ tests/ProjectIntegrationTestSuite.hpp | 17 ++++++++++++++ tests/main_test.cpp | 32 ++++----------------------- tests/test_functions.cpp | 10 +++++++++ tests/test_functions.hpp | 10 +++++++++ tests/unit_tests.cpp | 15 +++++++++++++ 7 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 tests/ProjectIntegrationTestSuite.cpp create mode 100644 tests/ProjectIntegrationTestSuite.hpp create mode 100644 tests/test_functions.cpp create mode 100644 tests/test_functions.hpp create mode 100644 tests/unit_tests.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f5b1680..5b8c2de 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,6 +15,11 @@ enable_testing() add_executable( ${PROJECT_NAME}_tests main_test.cpp + unit_tests.cpp + test_functions.cpp + test_functions.hpp + ProjectIntegrationTestSuite.cpp + ProjectIntegrationTestSuite.hpp ) target_link_libraries( diff --git a/tests/ProjectIntegrationTestSuite.cpp b/tests/ProjectIntegrationTestSuite.cpp new file mode 100644 index 0000000..66293bb --- /dev/null +++ b/tests/ProjectIntegrationTestSuite.cpp @@ -0,0 +1,9 @@ +#include "ProjectIntegrationTestSuite.hpp" + +void ProjectIntegrationTestSuite::SetUp() { + std::filesystem::create_directories(dirname); +} + +void ProjectIntegrationTestSuite::TearDown() { + std::filesystem::remove_all(dirname); +} diff --git a/tests/ProjectIntegrationTestSuite.hpp b/tests/ProjectIntegrationTestSuite.hpp new file mode 100644 index 0000000..75d677f --- /dev/null +++ b/tests/ProjectIntegrationTestSuite.hpp @@ -0,0 +1,17 @@ +#ifndef TEMPORARYDIRECTORYTESTSUITE_HPP_ +#define TEMPORARYDIRECTORYTESTSUITE_HPP_ + +#include <filesystem> +#include <string> + +#include <gtest/gtest.h> + +struct ProjectIntegrationTestSuite : public testing::Test { // special test structure + const std::string dirname = "./gtest_tmp"; + + void SetUp() override; // method that is called at the beginning of every test + + void TearDown() override; // method that is called at the end of every test +}; + +#endif //TEMPORARYDIRECTORYTESTSUITE_HPP_ diff --git a/tests/main_test.cpp b/tests/main_test.cpp index 5fe7fe3..16000a5 100644 --- a/tests/main_test.cpp +++ b/tests/main_test.cpp @@ -1,34 +1,10 @@ #include <sstream> -#include <filesystem> -#include <gtest/gtest.h> // include your library here +#include <gtest/gtest.h> +#include "ProjectIntegrationTestSuite.hpp" +#include "test_functions.hpp" // include your library here #include "lib/mylib/MyClass.h" -struct TemporaryDirectoryTestSuite : public testing::Test { // special test structure - const std::string dirname = "./gtest_tmp"; - - void SetUp() override { // method that is called at the beginning of every test - std::filesystem::create_directories(dirname); - } - - void TearDown() override { // method that is called at the end of every test - std::filesystem::remove_all(dirname); - } -}; - -std::vector<std::string> SplitString(const std::string& str) { - std::istringstream iss(str); - - return {std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()}; -} - -TEST_F(TemporaryDirectoryTestSuite, InitTest) { +TEST_F(ProjectIntegrationTestSuite, InitTest) { ASSERT_TRUE(std::filesystem::is_directory(dirname)); } - -TEST(MyLibUnitTestSuite, BasicTest1) { - std::ostringstream out; - MyClass printer(out); - printer.Print("Hello, World!"); - ASSERT_EQ(out.str(), "Hello, World!"); -} diff --git a/tests/test_functions.cpp b/tests/test_functions.cpp new file mode 100644 index 0000000..213536c --- /dev/null +++ b/tests/test_functions.cpp @@ -0,0 +1,10 @@ +#include <iterator> +#include <sstream> + +#include "test_functions.hpp" + +std::vector<std::string> SplitString(const std::string& str) { + std::istringstream iss(str); + + return {std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()}; +} \ No newline at end of file diff --git a/tests/test_functions.hpp b/tests/test_functions.hpp new file mode 100644 index 0000000..27d8e8b --- /dev/null +++ b/tests/test_functions.hpp @@ -0,0 +1,10 @@ +#ifndef TESTFUNCTIONS_HPP_ +#define TESTFUNCTIONS_HPP_ + +#include <iostream> +#include <vector> +#include <string> + +std::vector<std::string> SplitString(const std::string& str); + +#endif //TESTFUNCTIONS_HPP_ \ No newline at end of file diff --git a/tests/unit_tests.cpp b/tests/unit_tests.cpp new file mode 100644 index 0000000..27b0878 --- /dev/null +++ b/tests/unit_tests.cpp @@ -0,0 +1,15 @@ +#include <sstream> + +#include <gtest/gtest.h> +#include "test_functions.hpp" // include your library here +#include "lib/mylib/MyClass.h" + +TEST(MyLibUnitTestSuite, BasicTest1) { + std::ostringstream out; + MyClass printer(out); + printer.Print("Hello, World!"); + std::vector<std::string> out_by_words = SplitString(out.str()); + ASSERT_EQ(out_by_words.size(), 2); + ASSERT_EQ(out_by_words[0], "Hello,"); + ASSERT_EQ(out_by_words[1], "World!"); +} \ No newline at end of file