Skip to content

Commit

Permalink
Improve test example.
Browse files Browse the repository at this point in the history
Demonstrate usage of pytest_discover_tests function with multiple
dependent libraries and environment variables.
  • Loading branch information
buddly27 committed May 12, 2024
1 parent dc0efce commit 8de76e3
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: tests

on:
push:
branches: [ main, dev ]
branches: [ main ]

pull_request:
branches: [ main ]
Expand Down
4 changes: 4 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ find_package(Python COMPONENTS Interpreter Development REQUIRED)
set(_py_version ${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
mark_as_advanced(_py_version)

if (WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()

find_package(Boost 1.70.0 COMPONENTS "python${_py_version}" REQUIRED)

if (NOT TARGET Boost::python)
Expand Down
11 changes: 2 additions & 9 deletions example/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
add_library(example MODULE main.cpp)

set_target_properties(example PROPERTIES PREFIX "")

if(WIN32)
set_target_properties(example PROPERTIES SUFFIX ".pyd")
endif()

target_link_libraries(example PUBLIC Boost::python Python::Python)
add_subdirectory(foo)
add_subdirectory(python)
6 changes: 6 additions & 0 deletions example/src/foo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_library(foo SHARED foo.cpp)

target_include_directories(foo
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
40 changes: 40 additions & 0 deletions example/src/foo/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "./foo.h"

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdexcept>

Foo::Foo()
{
const char* settings = std::getenv("FOO_SETTINGS_FILE");
if (settings == nullptr) {
throw std::runtime_error("Environment variable FOO_SETTINGS_FILE is not set.");
}

std::ifstream file(settings);
if (!file.is_open()) {
throw std::runtime_error("Unable to open Foo file.");
}

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string lang, greeting;
if (std::getline(iss, lang, ':') && std::getline(iss, greeting)) {
_map[lang] = greeting;
}
}
file.close();
}

std::string Foo::sayHello(std::string language)
{
if (_map.find(language) == _map.end()) {
throw std::runtime_error("Language not found in Foo file.");
}

return _map[language];
}
18 changes: 18 additions & 0 deletions example/src/foo/foo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef EXAMPLE_FOO_H
#define EXAMPLE_FOO_H

#include <string>
#include <unordered_map>

class Foo {
public:
Foo();
virtual ~Foo() = default;

std::string sayHello(std::string language);

private:
std::unordered_map<std::string, std::string> _map;
};

#endif // EXAMPLE_FOO_H
32 changes: 32 additions & 0 deletions example/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
add_library(pyFoo MODULE main.cpp)

set_target_properties(pyFoo
PROPERTIES
PREFIX ""
OUTPUT_NAME foo
)

if(WIN32)
set_target_properties(pyFoo PROPERTIES SUFFIX ".pyd")
endif()

target_link_libraries(pyFoo
PUBLIC
foo
Boost::python
Python::Python
)

if (WIN32)
# As of Python v3.8 and newer, DLLs are no longer searched for in the
# PATH environment variable on Windows. Therefore, it is necessary to
# ensure that they are all located in the same directory.
add_custom_command(
TARGET pyFoo POST_BUILD
COMMAND ${CMAKE_COMMAND}
-E copy_if_different
$<TARGET_FILE:foo>
$<TARGET_FILE_DIR:pyFoo>
COMMAND_EXPAND_LISTS
)
endif()
11 changes: 7 additions & 4 deletions example/src/main.cpp → example/src/python/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include <boost/python.hpp>
#include <foo.h>

#include <cstdlib>
#include <string>

std::string greet(std::string name)
{
const char* value = std::getenv("GREETING_WORD");
Foo foo;

const char* value = std::getenv("DEFAULT_LANGUAGE");
if (value != nullptr)
return std::string(value) + ", " + name;
return foo.sayHello(value) + ", " + name;
else
return "bonjour, " + name;
return foo.sayHello("fr") + ", " + name;
}

BOOST_PYTHON_MODULE(example)
BOOST_PYTHON_MODULE(foo)
{
using namespace boost::python;
Py_Initialize();
Expand Down
12 changes: 6 additions & 6 deletions example/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pytest_discover_tests(
PythonTest
LIBRARY_PATH_PREPEND
$ENV{RUNNER_TEMP}
$<TARGET_FILE_DIR:example>
$<TARGET_FILE_DIR:foo>
$<TARGET_FILE_DIR:pyFoo>
PYTHON_PATH_PREPEND
$ENV{RUNNER_TEMP}
$<TARGET_FILE_DIR:example>
$<TARGET_FILE_DIR:pyFoo>
TRIM_FROM_NAME "^test_"
DEPENDS example
DEPENDS foo pyFoo
ENVIRONMENT
"GREETING_WORD=hello"
"DEFAULT_LANGUAGE=en"
"FOO_SETTINGS_FILE=${CMAKE_CURRENT_SOURCE_DIR}/resource/foo.txt"
)
3 changes: 3 additions & 0 deletions example/test/resource/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
en:hello
fr:bonjour
es:hola
16 changes: 13 additions & 3 deletions example/test/subfolder/test_example2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# -*- coding: utf-8 -*-
import foo

import example
import pytest


def test_greet_michael():
"""Greet Michael."""
assert example.greet("Michael") == "hello, Michael"
assert foo.greet("Michael") == "hello, Michael"


def test_greet_error(monkeypatch):
"""Impossible to greet when FOO settings is not found."""
monkeypatch.delenv("FOO_SETTINGS_FILE")

with pytest.raises(RuntimeError) as error:
foo.greet("Michael")

assert "Environment variable FOO_SETTINGS_FILE is not set" in str(error.value)
19 changes: 11 additions & 8 deletions example/test/test_example.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
# -*- coding: utf-8 -*-

import example
import foo


def test_greet_world():
"""Greet the world."""
assert example.greet() == "hello, world"
assert foo.greet() == "hello, world"


def test_greet_john():
"""Greet John."""
assert example.greet("John") == "hello, John"
assert foo.greet("John") == "hello, John"


def test_greet_julia():
"""Greet Julia."""
assert example.greet("Julia") == "hello, Julia"
assert foo.greet("Julia") == "hello, Julia"


def test_greet_julia_french(monkeypatch):
"""Greet Julia in French."""
monkeypatch.delenv("GREETING_WORD")
assert example.greet("Julia") == "bonjour, Julia"
monkeypatch.delenv("DEFAULT_LANGUAGE")
assert foo.greet("Julia") == "bonjour, Julia"


def test_greet_julia_spanish(monkeypatch):
"""Greet Julia in Spanish."""
monkeypatch.setenv("DEFAULT_LANGUAGE", "es")
assert foo.greet("Julia") == "hola, Julia"

0 comments on commit 8de76e3

Please sign in to comment.