GeezerVison is showing github.com — added example tests · olned/python_cpp_example@306a72d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ docs/_build | 4 | 4 |
| 5 | 5 | # Python | 6 | 6 | *.egg-info | | 7 | +__pycache__ | 7 | 8 |
| 8 | 9 | # VS Code | 9 | 10 | .vscode | |
Collapse file Copy file name to clipboardExpand all lines: CMakeLists.txt +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,19 @@ cmake_minimum_required(VERSION 2.8.12) | 2 | 2 | project(python_cpp_example) | 3 | 3 |
| 4 | 4 | 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") | 6 | 8 |
| | 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 | 7 | 19 | add_subdirectory(lib/pybind11) | 8 | 20 | pybind11_add_module(python_cpp_example ${SOURCES} "${SOURCE_DIR}/bindings.cpp") |
Collapse file Copy file name to clipboardExpand all lines: python_cpp_example/bindings.cpp +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include <pybind11/pybind11.h> | 2 | | -#include "main.hpp" | | 2 | +#include "math.hpp" | 3 | 3 |
| 4 | 4 | namespace py = pybind11; | 5 | 5 |
| |
Collapse file Copy file name to clipboardExpand all lines: python_cpp_example/math.cpp +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -#include "main.hpp" | | 1 | +#include "math.hpp" | 2 | 2 |
| 3 | 3 | int add(int i, int j) | 4 | 4 | { | |
Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboardExpand all lines: setup.py +28-3Lines changed: 28 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import os | 2 | 2 | import re | 3 | 3 | import sys | | 4 | +import sysconfig | 4 | 5 | import platform | 5 | 6 | import subprocess | 6 | 7 |
| | 8 | +from distutils.version import LooseVersion | 7 | 9 | from setuptools import setup, Extension | 8 | 10 | from setuptools.command.build_ext import build_ext | 9 | | -from distutils.version import LooseVersion | | 11 | +from setuptools.command.test import test as TestCommand | 10 | 12 |
| 11 | 13 |
| 12 | 14 | class CMakeExtension(Extension): | @@ -55,6 +57,29 @@ def build_extension(self, ext): | 55 | 57 | os.makedirs(self.build_temp) | 56 | 58 | subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env) | 57 | 59 | 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 | + | 58 | 83 |
| 59 | 84 | setup( | 60 | 85 | name='python_cpp_example', | @@ -64,6 +89,6 @@ def build_extension(self, ext): | 64 | 89 | description='A hybrid Python/C++ test project', | 65 | 90 | long_description='', | 66 | 91 | ext_modules=[CMakeExtension('python_cpp_example')], | 67 | | - cmdclass=dict(build_ext=CMakeBuild), | | 92 | + cmdclass=dict(build_ext=CMakeBuild, test=CatchTestCommand), | 68 | 93 | zip_safe=False, | 69 | | -) | | 94 | +) |
Collapse file Copy file name to clipboardExpand all lines: tests/__init__.py Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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() |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#define CATCH_CONFIG_MAIN | | 2 | +#include <catch.hpp> |
Collapse file Copy file name to clipboard +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#include <catch.hpp> | | 2 | + | | 3 | +#include "math.hpp" | | 4 | + | | 5 | +TEST_CASE("Addition and subtraction") | | 6 | +{ | | 7 | + REQUIRE(add(1, 1) == 2); | | 8 | +} |
|