Skip to content

Commit 306a72d

Browse files
committed
added example tests
1 parent ee14435 commit 306a72d

File tree

10 files changed

+67
-6
lines changed

10 files changed

+67
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ docs/_build
44

55
# Python
66
*.egg-info
7+
__pycache__
78

89
# VS Code
910
.vscode

CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@ cmake_minimum_required(VERSION 2.8.12)
22
project(python_cpp_example)
33

44
SET(SOURCE_DIR "python_cpp_example")
5-
SET(SOURCES "${SOURCE_DIR}/main.cpp")
5+
# Tell cmake that headers are in alse in source_dir
6+
include_directories(${SOURCE_DIR})
7+
SET(SOURCES "${SOURCE_DIR}/math.cpp")
68

9+
SET(TEST_DIR "tests")
10+
SET(TESTS ${SOURCES}
11+
"${TEST_DIR}/test_main.cpp"
12+
"${TEST_DIR}/test_math.cpp")
13+
14+
# Generate a test executable
15+
include_directories(lib/catch/include)
16+
add_executable("${PROJECT_NAME}_test" ${TESTS})
17+
18+
# Generate python module
719
add_subdirectory(lib/pybind11)
820
pybind11_add_module(python_cpp_example ${SOURCES} "${SOURCE_DIR}/bindings.cpp")

python_cpp_example/bindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <pybind11/pybind11.h>
2-
#include "main.hpp"
2+
#include "math.hpp"
33

44
namespace py = pybind11;
55

python_cpp_example/main.cpp renamed to python_cpp_example/math.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "main.hpp"
1+
#include "math.hpp"
22

33
int add(int i, int j)
44
{
File renamed without changes.

setup.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import os
22
import re
33
import sys
4+
import sysconfig
45
import platform
56
import subprocess
67

8+
from distutils.version import LooseVersion
79
from setuptools import setup, Extension
810
from setuptools.command.build_ext import build_ext
9-
from distutils.version import LooseVersion
11+
from setuptools.command.test import test as TestCommand
1012

1113

1214
class CMakeExtension(Extension):
@@ -55,6 +57,29 @@ def build_extension(self, ext):
5557
os.makedirs(self.build_temp)
5658
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
5759
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
60+
print()
61+
62+
class CatchTestCommand(TestCommand):
63+
"""
64+
A custom test runner to execute both python unittest tests and C++ Catch-
65+
lib tests.
66+
"""
67+
def distutils_dir_name(self, dname):
68+
"""Returns the name of a distutils build directory"""
69+
dir_name = "{dirname}.{platform}-{version[0]}.{version[1]}"
70+
return dir_name.format(dirname=dname,
71+
platform=sysconfig.get_platform(),
72+
version=sys.version_info)
73+
74+
def run(self):
75+
# Run python tests
76+
super(CatchTestCommand, self).run()
77+
print("\nPython tests complete, now running C++ tests...\n")
78+
# Run catch tests
79+
subprocess.call(['./python_cpp_example_test'],
80+
cwd=os.path.join('build',
81+
self.distutils_dir_name('temp')))
82+
5883

5984
setup(
6085
name='python_cpp_example',
@@ -64,6 +89,6 @@ def build_extension(self, ext):
6489
description='A hybrid Python/C++ test project',
6590
long_description='',
6691
ext_modules=[CMakeExtension('python_cpp_example')],
67-
cmdclass=dict(build_ext=CMakeBuild),
92+
cmdclass=dict(build_ext=CMakeBuild, test=CatchTestCommand),
6893
zip_safe=False,
69-
)
94+
)

tests/__init__.py

Whitespace-only changes.

tests/math_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Test simulation
2+
import unittest
3+
import python_cpp_example
4+
5+
class MainTest(unittest.TestCase):
6+
def test_add(self):
7+
self.assertEqual(python_cpp_example.add(1, 1), 2)
8+
9+
def test_subtract(self):
10+
self.assertEqual(python_cpp_example.subtract(1, 1), 0)
11+
12+
if __name__ == '__main__':
13+
unittest.main()

tests/test_main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define CATCH_CONFIG_MAIN
2+
#include <catch.hpp>

tests/test_math.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <catch.hpp>
2+
3+
#include "math.hpp"
4+
5+
TEST_CASE("Addition and subtraction")
6+
{
7+
REQUIRE(add(1, 1) == 2);
8+
}

0 commit comments

Comments
 (0)