4.3. Step - 01 : Setup of C/C++ Code

Before we start running BDD Tests, we need to setup C/C++ Code first, and ensure our C/C++ compiles.

We will have the following initial directory structure:

.
|-- features
|   |-- step_definitions
|   |   `-- steps_addition.cpp
|   `-- CMakeLists.txt
`-- CMakeLists.txt

The contents of the files as under for the time being.

  • features/step_definitions/steps_addition.cpp

    This is the file would eventually have the Step Definitions. The contents of the file as as under. As of now empty.

    // features/step_definitions/steps_addition.cpp
    //
    // Just an Empty CPP File to allow compilation.
    
  • features/CMakeLists.txt

    This is the file creates the feature executable. We use CMake for compilation. The file looks like like this.

    PROJECT(features)
    
    FILE(
        GLOB
        all_files
        step_definitions/*.cpp
        step_definitions/*.hpp
    )
    
    ADD_EXECUTABLE(${PROJECT_NAME} ${all_files})
    
    TARGET_LINK_LIBRARIES(
        ${PROJECT_NAME}
        CucumberCpp::cucumber-cpp
    )
    
  • <TOP>/CMakeLists.txt

    The top level CMakeLists.txt is required for the project/solution.

    CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
    
    PROJECT(DemoBDD)
    
    INCLUDE(/usr/local/lib/cmake/CucumberCppConfig.cmake)
    
    SET(BOOST_MIN_VERSION "1.51")
    
    SET(CUKE_CORE_BOOST_LIBS
        thread
        system
        regex
        date_time
        program_options
        filesystem
    )
    FIND_PACKAGE(
        Boost
        ${BOOST_MIN_VERSION}
        COMPONENTS ${CUKE_CORE_BOOST_LIBS}
        REQUIRED
    )
    
    FIND_PACKAGE(
        GMock
        REQUIRED
    )
    FIND_PACKAGE(
        GTest
        REQUIRED
    )
    
    ADD_SUBDIRECTORY(features)
    

4.3.1. Use CMake

Using CMake, if we configure a work space, the command looks like this:

cmake -S . -B ../_01-setup-cpp-code

If everything is setup perfectly, the output looks like this:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.51") found components: thread system regex date_time program_options filesystem 
-- Found Threads: TRUE  
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2") 
-- Using /usr/src/googletest/googlemock/ as gmock source directory
-- Checking for module 'gtest'
--   Found gtest, version 1.11.0
-- Using GTest v1.11.0 (parsed from pkg-config)
-- Found GTest: /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake (found version "1.11.0")  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c6h6/data/p/book-tests/pgh_tech-bdd/book/csrc/_01-setup-cpp-code

4.3.2. Compile the feature executable

Since CMake has created workspace based on makefile, if we run:

make all

If everything is setup perfectly, the output looks like this:

[ 10%] Creating directories for 'GMock'
[ 20%] No download step for 'GMock'
[ 30%] No update step for 'GMock'
[ 40%] No patch step for 'GMock'
[ 50%] Performing configure step for 'GMock'
-- The CXX compiler identification is GNU 11.3.0
-- The C compiler identification is GNU 11.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found Python: /home/c6h6/.virtualenvs/ratuenv/bin/python3 (found version "3.10.6") found components: Interpreter 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c6h6/data/p/book-tests/pgh_tech-bdd/book/csrc/_01-setup-cpp-code/gmock
[ 60%] Performing build step for 'GMock'
[ 12%] Building CXX object /home/c6h6/data/p/book-tests/pgh_tech-bdd/book/csrc/_01-setup-cpp-code/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 25%] Linking CXX static library /home/c6h6/data/p/book-tests/pgh_tech-bdd/book/csrc/_01-setup-cpp-code/gmock/lib/libgtest.a
[ 25%] Built target gtest
[ 37%] Building CXX object CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 50%] Linking CXX static library lib/libgmock.a
[ 50%] Built target gmock
[ 62%] Building CXX object CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 75%] Linking CXX static library lib/libgmock_main.a
[ 75%] Built target gmock_main
[ 87%] Building CXX object /home/c6h6/data/p/book-tests/pgh_tech-bdd/book/csrc/_01-setup-cpp-code/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library /home/c6h6/data/p/book-tests/pgh_tech-bdd/book/csrc/_01-setup-cpp-code/gmock/lib/libgtest_main.a
[100%] Built target gtest_main
[ 70%] No install step for 'GMock'
[ 80%] Completed 'GMock'
[ 80%] Built target GMock
[ 90%] Building CXX object features/CMakeFiles/features.dir/step_definitions/steps_addition.cpp.o
[100%] Linking CXX executable features
[100%] Built target features

And, we must have a feature executable generated.

4.3.3. Executing the generated executable

We are not yet ready to run any tests, but let’s just run help command over the generated executable:

./features/features --help

If everything is setup perfectly, the output looks like this:

Allowed options:
  -h [ --help ]         help for cucumber-cpp
  -v [ --verbose ]      verbose output
  -l [ --listen ] arg   listening address of wireserver
  -p [ --port ] arg     listening port of wireserver, use '0' (zero) to select 
                        an ephemeral port
  -u [ --unix ] arg     listening unix socket of wireserver (disables listening
                        on port)