# Make testable the encapsulated code of a library

**URL:** https://discourse.cmake.org/t/make-testable-the-encapsulated-code-of-a-library/10887
**Category:** Code
**Created:** [May 22, 2024, 4:05pm UTC](https://discourse.cmake.org/t/make-testable-the-encapsulated-code-of-a-library/10887 "2024-05-22T16:05:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [May 22, 2024, 4:05pm UTC](https://discourse.cmake.org/t/make-testable-the-encapsulated-code-of-a-library/10887/1 "2024-05-22T16:05:39Z")

</div>

Hello,

I made a library, and I followed the classical pattern where the implementation is put in a **src** directory, and the interface in a **include/MyLib** directory. So the consumers may refer to functions exposed in the latter, and link to the source compiled from the former.

But now I’d like to test some code of the implementation. I mean free functions, and public member functions which are not exposed in the interface.

I added a **tests** directory. So, in the CMakeLists.txt at the root directory, I have these two commands :

```auto
add_subdirectory(src)
...
add_subdirectory(tests)

```

In the tests directory, I created a CMakeLists.txt whose content is:

```auto
add_executable(MyLibTest test.cpp)

target_include_directories(MyLibTest 
    PRIVATE
        ${PROJECT_SOURCE_DIR}/include
        ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_directories(MyLibTest 
    PRIVATE
        MyLib)

add_test(FirstTest MyLibTest )

```

Is it the good way ?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 23, 2024, 10:55am UTC](https://discourse.cmake.org/t/make-testable-the-encapsulated-code-of-a-library/10887/2 "2024-05-23T10:55:48Z")

</div>

Duplicate of [Make some library's code private some consumers, but public for tests](https://discourse.cmake.org/t/make-some-librarys-code-private-some-consumers-but-public-for-tests/10886)

Locking.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 23, 2024, 10:55am UTC](https://discourse.cmake.org/t/make-testable-the-encapsulated-code-of-a-library/10887/3 "2024-05-23T10:55:58Z")

</div>


