test_package & Consuming Your Package
A package that builds isn't necessarily a package that works for consumers: the headers may not be installed, package_info() may name the wrong library, a transitive dependency may be missing. Conan's answer is the test_package: a tiny consumer project inside the recipe folder that conan create builds and runs against the freshly created package.
What test_package is (and isn't)
- It's a consumer, written exactly like your users' projects:
find_package(),target_link_libraries(), amain(). - It checks packaging, not behaviour. One call into the library is enough; keep your unit tests in your library's own build.
- Its conanfile requires
self.tested_reference_str, the referenceconan createjust built.
A test_package for textfmt
The recipe below is the textfmt library from the previous lesson, shortened. The new part is the test_package/ folder:
mkdir -p textfmt/src textfmt/include/textfmt textfmt/test_package && cd textfmt
cat > conanfile.py <<'PY'
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
class TextFmtRecipe(ConanFile):
name = "textfmt"
version = "0.3.0"
package_type = "static-library"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "CMakeLists.txt", "src/*", "include/*"
def requirements(self):
self.requires("fmt/12.1.0", transitive_headers=True)
def layout(self):
cmake_layout(self)
def build(self):
cmake = CMake(self); cmake.configure(); cmake.build()
def package(self):
CMake(self).install()
def package_info(self):
self.cpp_info.libs = ["textfmt"]
self.cpp_info.set_property("cmake_file_name", "TextFmt")
self.cpp_info.set_property("cmake_target_name", "TextFmt::textfmt")
PY
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(textfmt CXX)
find_package(fmt REQUIRED CONFIG)
add_library(textfmt src/table.cpp)
target_include_directories(textfmt PUBLIC include)
target_link_libraries(textfmt PUBLIC fmt::fmt)
install(TARGETS textfmt)
install(DIRECTORY include/ DESTINATION include)
TXT
cat > include/textfmt/table.h <<'SRC'
#pragma once
#include <string>
#include <utility>
#include <vector>
namespace textfmt { std::string table(const std::vector<std::pair<std::string, int>>& rows); }
SRC
cat > src/table.cpp <<'SRC'
#include "textfmt/table.h"
#include <fmt/format.h>
std::string textfmt::table(const std::vector<std::pair<std::string, int>>& rows) {
std::string out;
for (auto& [k, v] : rows) out += fmt::format("|{:<8}|{:>5}|\n", k, v);
return out;
}
SRC
# --- the test package
cat > test_package/conanfile.py <<'PY'
import os
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
class TestPackage(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain"
def requirements(self):
self.requires(self.tested_reference_str)
def layout(self):
cmake_layout(self)
def build(self):
cmake = CMake(self); cmake.configure(); cmake.build()
def test(self):
if can_run(self):
self.run(os.path.join(self.cpp.build.bindir, "example"), env="conanrun")
PY
cat > test_package/CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(test_package CXX)
find_package(TextFmt REQUIRED CONFIG)
add_executable(example example.cpp)
target_link_libraries(example PRIVATE TextFmt::textfmt)
TXT
cat > test_package/example.cpp <<'SRC'
#include <cstdio>
#include "textfmt/table.h"
int main() { std::fputs(textfmt::table({{"tests", 1}, {"passed", 1}}).c_str(), stdout); }
SRC
conan create . -c tools.build:jobs=1 2>&1 | sed -n '/Testing the package: Executing test/,$p'======== Testing the package: Executing test ========
textfmt/0.3.0 (test package): Running test()
textfmt/0.3.0 (test package): RUN: ./example
|tests | 1|
|passed | 1|
can_run(self) returns false when cross-compiling (you can't run an ARM binary on an x86 build machine), so the test still builds but doesn't execute there.
You can re-run just the test against an existing package with conan test test_package textfmt/0.3.0.
Consuming the package from an application
Once conan create has put textfmt/0.3.0 into the cache, any project on the machine can require it. After an upload, any machine can:
mkdir -p textfmt/src textfmt/include/textfmt app && cd textfmt
cat > conanfile.py <<'PY'
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
class TextFmtRecipe(ConanFile):
name = "textfmt"
version = "0.3.0"
package_type = "static-library"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "CMakeLists.txt", "src/*", "include/*"
def requirements(self):
self.requires("fmt/12.1.0")
def layout(self):
cmake_layout(self)
def build(self):
cmake = CMake(self); cmake.configure(); cmake.build()
def package(self):
CMake(self).install()
def package_info(self):
self.cpp_info.libs = ["textfmt"]
self.cpp_info.set_property("cmake_file_name", "TextFmt")
self.cpp_info.set_property("cmake_target_name", "TextFmt::textfmt")
PY
printf 'cmake_minimum_required(VERSION 3.25)\nproject(textfmt CXX)\nfind_package(fmt REQUIRED CONFIG)\nadd_library(textfmt src/table.cpp)\ntarget_include_directories(textfmt PUBLIC include)\ntarget_link_libraries(textfmt PRIVATE fmt::fmt)\ninstall(TARGETS textfmt)\ninstall(DIRECTORY include/ DESTINATION include)\n' > CMakeLists.txt
printf '#pragma once\n#include <string>\nnamespace textfmt { std::string banner(const std::string& s); }\n' > include/textfmt/table.h
printf '#include "textfmt/table.h"\n#include <fmt/format.h>\nstd::string textfmt::banner(const std::string& s) { return fmt::format("*** {:^14} ***", s); }\n' > src/table.cpp
conan create . --test-folder="" -c tools.build:jobs=1 > /dev/null 2>&1
cd ../app
cat > conanfile.txt <<'TXT'
[requires]
textfmt/0.3.0
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
TXT
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(app CXX)
find_package(TextFmt REQUIRED CONFIG)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE TextFmt::textfmt)
TXT
printf '#include <cstdio>\n#include "textfmt/table.h"\nint main() { std::puts(textfmt::banner("release").c_str()); }\n' > main.cpp
conan install . -c tools.build:jobs=1 2>&1 | sed -n '/Computing dependency graph/,/^$/p'
cmake --preset conan-release > /dev/null && cmake --build --preset conan-release > /dev/null && ./build/Release/app======== Computing dependency graph ========
Graph root
conanfile.txt: /home/learner/app/conanfile.txt
Requirements
fmt/12.1.0#6baf0fb8351783472b94ee6e36232391 - Cache
textfmt/0.3.0#bc7d589407a7b10381618e068f9364da - Cache
*** release ***
This time textfmt uses fmt only privately (inside table.cpp), so it doesn't set transitive_headers. The graph still contains fmt: the app has to link libfmt.a because libtextfmt.a calls into it, but it doesn't get fmt's headers.
Working on a package and its consumer together: editable mode
Re-running conan create after every change to textfmt is slow when you're developing it side by side with an app. Editable mode points the cache at your working folder instead:
conan editable add ./textfmt # textfmt/0.3.0 now resolves to ./textfmt
cd textfmt && conan build . # build it in place
cd ../app && conan install . && cmake --build --preset conan-release
conan editable list
conan editable remove ./textfmt # back to the cached packageThe consumer's CMakeDeps files then point into textfmt/build/Release and textfmt/include, so a rebuild of the library is picked up immediately. Remember to remove the editable before you create release packages.
Test yourself
What should a test_package verify?
- Every unit test of the library
- That a consumer can find, compile against, link and run the package
- That the source downloads
- The code style
Why does a test_package call can_run(self) before running the example?
- To save time
- When cross-compiling, the built example can't run on the build machine
- To check the license
- can_run is required by CMake
What does conan editable add ./textfmt do?
- Uploads textfmt
- Makes textfmt/0.3.0 resolve to your working folder instead of the cache
- Deletes the cached package
- Opens the recipe in an editor