-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrate usage of pytest_discover_tests function with multiple dependent libraries and environment variables.
- Loading branch information
Showing
12 changed files
with
143 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ name: tests | |
|
||
on: | ||
push: | ||
branches: [ main, dev ] | ||
branches: [ main ] | ||
|
||
pull_request: | ||
branches: [ main ] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
en:hello | ||
fr:bonjour | ||
es:hola |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |