11import os
22import re
33import sys
4+ import sysconfig
45import platform
56import subprocess
67
8+ from distutils .version import LooseVersion
79from setuptools import setup , Extension
810from setuptools .command .build_ext import build_ext
9- from distutils . version import LooseVersion
11+ from setuptools . command . test import test as TestCommand
1012
1113
1214class 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 ("\n Python 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
5984setup (
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+ )
0 commit comments