Skip to content

Commit

Permalink
Add Google Test demo
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Sep 25, 2023
1 parent d01a073 commit d7dc97b
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ CMakeCache.txt
**/Testing/Temporary/**
DartConfiguration.tcl
build/
/_deps
/lib

# Generated files
testcontainers-c/testcontainers-c.h
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ Coming soon: guidelines, specs and code documentation. Check out the examples fo

## Usage in C/C++

See [the examples and demos](./demo/)
- [Using the generic Testcontainer C API](./demo/generic-container/)
- [Using Testcontainers C in Google Test (C++)](./demo/google-test/)
- [Using the WireMock module](./demo/wiremock/)

See [the examples and demos](./demo/) for more examples.

## Usage in other languages

Expand Down Expand Up @@ -206,11 +210,6 @@ You are welcome to contribute more modules in this or a standalone repository!
> If you develop new modules, once `vcpkg` or `Conan` packaging is implemented for Testcontainers C,
> you might want to develop your module in a standalone repository instead.
## Examples and Demos

- [Using the generic Testcontainer C API](./demo/generic-container/)
- [Using the WireMock module](./demo/wiremock/)

## Credits

Using a complex Golang framework from C/C++ is not trivial.
Expand Down
1 change: 1 addition & 0 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Builds Demos with embedded tests
add_subdirectory(generic-container)
add_subdirectory(wiremock)
add_subdirectory(google-test)
1 change: 0 additions & 1 deletion demo/generic-container/generic_container_demo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <string.h>
#include "testcontainers-c.h"

#define DEFAULT_IMAGE "wiremock/wiremock:3.0.1-1"
Expand Down
29 changes: 29 additions & 0 deletions demo/google-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Google Test demo
# This is based on https://google.github.io/googletest/quickstart-cmake.html
project(google-test-demo
VERSION 0.0.1
DESCRIPTION "Demonstrates usage of Testcontainers C in Google Test")

set(TARGET_OUT ${PROJECT_NAME}.out)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()
file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

add_executable(${TARGET_OUT} test.cpp)
add_dependencies(${TARGET_OUT} testcontainers-c-shim)
target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c)
target_link_libraries(${TARGET_OUT} PRIVATE GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(${TARGET_OUT})
28 changes: 28 additions & 0 deletions demo/google-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Using Testcontainers C in Google Test

Demonstrates usage of Testcontainers C in [Google Test](https://github.com/google/googletest).
See [test.cpp](./test.cpp) for the code.

## Run the demo

```bash
cmake .
cmake --build .
cd demo/google-test
ctest --output-on-failure
```

## Sample output

```shell
onenashev:~/testcontainers-c/demo/google-test$ ctest --output-on-failure
Test project /home/onenashev/testcontainers-c/demo/google-test
Start 1: WireMockTestContainer.HelloWorld
1/5 Test #1: WireMockTestContainer.HelloWorld ...................... Passed 3.31 sec
Start 2: WireMockTestContainer.HelloWorldFromResource
2/5 Test #2: WireMockTestContainer.HelloWorldFromResource .......... Passed 3.97 sec
Start 3: WireMockTestContainer.HelloWorldFromMissingResource
3/5 Test #3: WireMockTestContainer.HelloWorldFromMissingResource ... Passed 3.91 sec

100% tests passed, 0 tests failed out of 3
```
67 changes: 67 additions & 0 deletions demo/google-test/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include "testcontainers-c.h"

class WireMockTestContainer : public ::testing::Test {

const char* WIREMOCK_IMAGE = "wiremock/wiremock:3.0.1-1";
const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings";

protected:
void SetUp() override {
std::cout << "Creating new container: " << WIREMOCK_IMAGE << '\n';

int requestId = tc_new_container_request(WIREMOCK_IMAGE);
tc_with_exposed_tcp_port(requestId, 8080);
tc_with_wait_for_http(requestId, 8080, WIREMOCK_ADMIN_MAPPING_ENDPOINT);
tc_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json");
tc_with_file(requestId, "test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json");
tc_with_file(requestId, "test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json");
tc_with_file(requestId, "test_data/response.xml", "/home/wiremock/__files/response.xml");

struct tc_run_container_return ret = tc_run_container(requestId);
containerId = ret.r0;

EXPECT_TRUE(ret.r1) << "Failed to run the container: " << ret.r2;
};

void TearDown() override {
// Code here will be called immediately after each test (right
// before the destructor).
};

int containerId;
};

TEST_F(WireMockTestContainer, HelloWorld) {
std::cout << "Sending HTTP request to the container\n";
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello");

ASSERT_NE(response.r0, -1) << "Failed to send HTTP request: " << response.r2;
ASSERT_EQ(response.r0, 200) << "Received wrong response code: " << response.r1 << response.r2;

std::cout << "Server Response: HTTP-" << response.r0 << '\n' << response.r1 << '\n';
}

TEST_F(WireMockTestContainer, HelloWorldFromResource) {
std::cout << "Sending HTTP request to the container\n";
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello-from-resource");

ASSERT_NE(response.r0, -1) << "Failed to send HTTP request: " << response.r2;
ASSERT_EQ(response.r0, 200) << "Received wrong response code: " << response.r1 << response.r2;

std::cout << "Server Response: HTTP-" << response.r0 << '\n' << response.r1 << '\n';
}

TEST_F(WireMockTestContainer, HelloWorldFromMissingResource) {
std::cout << "Sending HTTP request to the container\n";
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello-from-missing-resource");

ASSERT_EQ(response.r0, 500) << "The request should have failed";
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
12 changes: 12 additions & 0 deletions demo/google-test/test_data/hello.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"request": {
"method": "GET",
"url": "/hello"
},

"response": {
"status": 200,
"body": "Hello, world!"
}
}

10 changes: 10 additions & 0 deletions demo/google-test/test_data/hello_with_missing_resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"request": {
"method": "GET",
"url": "/hello-from-missing-resource"
},
"response": {
"status": 200,
"bodyFileName": "response_missing.xml"
}
}
10 changes: 10 additions & 0 deletions demo/google-test/test_data/hello_with_resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"request": {
"method": "GET",
"url": "/hello-from-resource"
},
"response": {
"status": 200,
"bodyFileName": "response.xml"
}
}
6 changes: 6 additions & 0 deletions demo/google-test/test_data/response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<note>
<to>you</to>
<from>WireMock</from>
<heading>Response</heading>
<body>Hello, world!</body>
</note>

0 comments on commit d7dc97b

Please sign in to comment.