Created
July 24, 2018 11:20
-
-
Save elena-roff/eeee6ba3ba0beea4794cd7d990154906 to your computer and use it in GitHub Desktop.
Working with data: run tests and coverage
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
import os | |
import unittest | |
import shutil | |
from setuptools import setup | |
from setuptools import Command | |
from dashboard import __version__ | |
try: | |
import coverage | |
except ImportError: | |
print("Coverage package is not installed yet. Setup will take care of it.") | |
# CONSTANTS | |
PACKAGE = 'package_name' | |
SOURCE_DIR = PACKAGE | |
TEST_DIR = 'tests' | |
def readme(): | |
with open("README.md") as fh: | |
return fh.read() | |
def my_test_suite(): | |
test_loader = unittest.TestLoader() | |
# runs files starting with "test_" | |
test_suite = test_loader.discover(os.path.join(SOURCE_DIR, TEST_DIR), pattern="test_*.py") | |
return test_suite | |
class CoverageCommand(Command): | |
""" Test Coverage Analysis Command. """ | |
description = "Test Coverage Analysis" | |
user_options = [] | |
def initialize_options(self): | |
""" no options """ | |
pass | |
def finalize_options(self): | |
""" no options """ | |
pass | |
def run(self): | |
""" main command with the logic """ | |
try: | |
shutil.rmtree("test/data") | |
except: | |
# do not handle exceptions | |
pass | |
test_suite = my_test_suite() | |
runner = unittest.TextTestRunner() | |
cov = coverage.Coverage(branch=True, source=".") | |
cov.start() | |
runner.run(test_suite) | |
cov.stop() | |
cov.report(include="{}/tools*".format(PACKAGE), show_missing=False) | |
setup( | |
name = PACKAGE, | |
# make sure to have __version__ in __init__.py | |
version = __version__, | |
author = "name", | |
author_email = "[email protected]", | |
packages = [PACKAGE], | |
# example of packages | |
install_requires = ['pandas>=0.22.0', | |
'numpy==1.14.1' | |
], | |
setup_requires = ['coverage==(version)'], | |
tests_require = ['coverage==(version)'], | |
description = "package_description", | |
long_description = readme(), | |
url = "path_to_the_package", | |
test_suite = 'setup.my_test_suite', | |
include_package_data = True, | |
classifiers = [], | |
dependency_links = ["url_to_artifactory"], | |
entry_points = {"distutils.commands": ["cover = setup:CoverageCommand"]} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment