Skip to content

Commit

Permalink
Python: Simplify test suite generation by using unittest discovery (#485
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nicksay authored and eustas committed Dec 20, 2016
1 parent 6f22722 commit 89a5b6e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 34 deletions.
16 changes: 12 additions & 4 deletions .gitignore
@@ -1,9 +1,17 @@
# C
*.o
*.pyc
*.txt.uncompressed
*.bro
*.unbro
bin/
buildfiles/
**/obj/
dist/

# Python
__pycache__/
*.py[cod]
*.so
*.egg-info/

# Tests
*.txt.uncompressed
*.bro
*.unbro
Empty file added python/tests/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion python/tests/bro_test.py
Expand Up @@ -6,7 +6,7 @@
import subprocess
import unittest

import _test_utils
from . import _test_utils
import brotli

PYTHON = _test_utils.PYTHON
Expand Down
2 changes: 1 addition & 1 deletion python/tests/compress_test.py
Expand Up @@ -5,7 +5,7 @@

import unittest

import _test_utils
from . import _test_utils
import brotli


Expand Down
2 changes: 1 addition & 1 deletion python/tests/compressor_test.py
Expand Up @@ -6,7 +6,7 @@
import functools
import unittest

import _test_utils
from . import _test_utils
import brotli


Expand Down
2 changes: 1 addition & 1 deletion python/tests/decompress_test.py
Expand Up @@ -5,7 +5,7 @@

import unittest

import _test_utils
from . import _test_utils
import brotli


Expand Down
35 changes: 9 additions & 26 deletions setup.py
Expand Up @@ -6,14 +6,15 @@
import os
import platform
import re
import unittest

try:
from setuptools import Extension
from setuptools import setup
except:
from distutils.core import Extension
from distutils.core import setup
from distutils.command.build_ext import build_ext
from distutils.cmd import Command


CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Expand All @@ -37,30 +38,10 @@ def get_version():
return '{0}.{1}.{2}'.format(major, minor, patch)


class TestCommand(Command):
""" Run all *_test.py scripts in 'tests' folder with the same Python
interpreter used to run setup.py.
"""

user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
import sys, subprocess, glob

test_dir = os.path.join(CURR_DIR, 'python', 'tests')
os.chdir(test_dir)

for test in glob.glob('*_test.py'):
try:
subprocess.check_call([sys.executable, test])
except subprocess.CalledProcessError:
raise SystemExit(1)
def get_test_suite():
test_loader = unittest.TestLoader()
test_suite = test_loader.discover('python', pattern='*_test.py')
return test_suite


class BuildExt(build_ext):
Expand Down Expand Up @@ -258,9 +239,10 @@ def build_extension(self, ext):
language='c++'),
]

TEST_SUITE = 'setup.get_test_suite'

CMD_CLASS = {
'build_ext': BuildExt,
'test': TestCommand,
}

setup(
Expand All @@ -275,4 +257,5 @@ def build_extension(self, ext):
package_dir=PACKAGE_DIR,
py_modules=PY_MODULES,
ext_modules=EXT_MODULES,
test_suite=TEST_SUITE,
cmdclass=CMD_CLASS)

0 comments on commit 89a5b6e

Please sign in to comment.