Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script for running all tests from inner buildout packages #70

Open
wants to merge 2 commits into
base: ea_a499878011598746_separating_awarding
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import sys

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example usage додати сюди і README.

import os
import nose
import argparse
from pkg_resources import iter_entry_points
import logging

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8

DEFAULT_PACKAGES = ['openprocurement'] # default packages that should be tested
DEFAULT_RECURSION_LIMIT = 1000
NOSE_ENV = {
"NOSE_WITH_XUNIT": 1,
"NOSE_WITH_COVERAGE": 1,
"NOSE_COVER_PACKAGE": [],
"NOSE_COVER_ERASE": 1,
"NOSE_COVER_HTML": 1,
"NOSE_PROCESSES": 0,
"NOSE_WITH_DOCTEST": 1,
"NOSE_NOCAPTURE": 1,
"NOSE_VERBOSE": 1,
}
os.environ['PYTEST_ADDOPTS'] = '--ignore=src/ --junitxml=pytest.xml'


def set_recursion_limit(tests_amount):
"""
Increase default python recursion depth limit
if tests amount is greater than that value
"""
if (DEFAULT_RECURSION_LIMIT - tests_amount) < DEFAULT_RECURSION_LIMIT / 2:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Якщо більше половини, то ми збільшуємо ? Чому саме так ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Немає фіксованої залежності глибини рекурсії від кількості тестів.
Такий варіант здався оптимальним.

sys.setrecursionlimit(tests_amount + DEFAULT_RECURSION_LIMIT)


def get_tests(packages, test_type):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring

tested_packages = list()
all_tests = list()
for pack in packages:
group = pack.split('.')[0] if pack.split('.')[0:1] else None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Якась трохи дивна перевірка. Які значення можуть бути тут pack.split('.')[0:1]?

if group is None:
continue
for entry_point in iter_entry_points(group=group + '.' + test_type):
package_name = group + '.{}'
package_name = package_name.format(entry_point.name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В одному format буде краще виглядати.

tested_packages.append(package_name)
suite = entry_point.load()
if package_name.startswith(pack):
if test_type == 'tests':
for tests in suite():
all_tests.append(tests)
elif test_type == 'pytests':
all_tests.append(suite)
return all_tests, tested_packages


def unpack_suites(suites):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring

tests = list()
for suite in suites:
if hasattr(suite, '_tests'):
tests += unpack_suites(suite._tests)
else:
tests.append(suite)
return tests


if __name__ == '__main__':
logger = logging.getLogger('console_output')
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
logger.addHandler(console_handler)
parser = argparse.ArgumentParser(description='Test some packages.')
parser.add_argument('packages', metavar='P', type=str, nargs='*',
help='name of packages to test', default=DEFAULT_PACKAGES)
parser.add_argument('--list-packages', dest='list_packages', action='store_const',
const=True, default=False,
help='List packages that can be tested')
args = parser.parse_args()
unique_packages = set(args.packages)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Все це варто в окрему функцію main закинути

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

І тут викликати.

nose_tests, cover_packages = get_tests(unique_packages, 'tests')
pytests, pytest_packages = get_tests(unique_packages, 'pytests')
if args.list_packages:
logger.info('NOSETESTS:')
for p in cover_packages:
logger.info("> {}".format(p))
logger.info('PYTESTS:')
for p in pytest_packages:
logger.info("> {}".format(p))
exit()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Навіщо тут exit()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yarsanich, для того, щоб зупинити виконання скрипта. Опція --list-packages призначена для того, щоб переглянути пакети, які будуть протестовані.

NOSE_ENV['NOSE_COVER_PACKAGE'] = cover_packages
unpacked_tests = unpack_suites(nose_tests)
set_recursion_limit(len(unpacked_tests))
sys.exit([suite() for suite in pytests],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generator expression тут не підійде ?

nose.run_exit(suite=unpacked_tests, env=NOSE_ENV))