bok_choy.py 4.34 KB
Newer Older
1 2 3 4 5 6 7
"""
Run acceptance tests that use the bok-choy framework
http://bok-choy.readthedocs.org/en/latest/
"""
from paver.easy import task, needs, cmdopts, sh
from pavelib.utils.test.suites.bokchoy_suite import BokChoyTestSuite
from pavelib.utils.envs import Env
Minh Tue Vo committed
8
from pavelib.utils.test.utils import check_firefox_version
9
from optparse import make_option
10
import os
11 12 13 14 15 16 17 18 19 20 21 22 23 24

try:
    from pygments.console import colorize
except ImportError:
    colorize = lambda color, text: text  # pylint: disable-msg=invalid-name

__test__ = False  # do not collect


@task
@needs('pavelib.prereqs.install_prereqs')
@cmdopts([
    ('test_spec=', 't', 'Specific test to run'),
    ('fasttest', 'a', 'Skip some setup'),
25
    ('extra_args=', 'e', 'adds as extra args to the test command'),
26
    ('default_store=', 's', 'Default modulestore'),
27 28 29
    make_option("--verbose", action="store_const", const=2, dest="verbosity"),
    make_option("-q", "--quiet", action="store_const", const=0, dest="verbosity"),
    make_option("-v", "--verbosity", action="count", dest="verbosity"),
Minh Tue Vo committed
30
    make_option("--skip_firefox_version_validation", action='store_false', dest="validate_firefox_version")
31 32 33 34 35 36 37 38 39 40 41 42 43
])
def test_bokchoy(options):
    """
    Run acceptance tests that use the bok-choy framework.
    Skips some setup if `fasttest` is True.

    `test_spec` is a nose-style test specifier relative to the test directory
    Examples:
    - path/to/test.py
    - path/to/test.py:TestFoo
    - path/to/test.py:TestFoo.test_bar
    It can also be left blank to run all tests in the suite.
    """
44 45 46 47 48 49
    # Note: Bok Choy uses firefox if SELENIUM_BROWSER is not set. So we are using
    # firefox as the default here.
    using_firefox = (os.environ.get('SELENIUM_BROWSER', 'firefox') == 'firefox')
    validate_firefox = getattr(options, 'validate_firefox_version', using_firefox)

    if validate_firefox:
Minh Tue Vo committed
50
        check_firefox_version()
51

52 53 54
    opts = {
        'test_spec': getattr(options, 'test_spec', None),
        'fasttest': getattr(options, 'fasttest', False),
55
        'default_store': getattr(options, 'default_store', None),
56 57
        'verbosity': getattr(options, 'verbosity', 2),
        'extra_args': getattr(options, 'extra_args', ''),
58 59
        'test_dir': 'tests',
    }
60
    run_bokchoy(**opts)
61 62 63 64 65 66 67 68


@task
@needs('pavelib.prereqs.install_prereqs')
@cmdopts([
    ('test_spec=', 't', 'Specific test to run'),
    ('fasttest', 'a', 'Skip some setup'),
    ('imports_dir=', 'd', 'Directory containing (un-archived) courses to be imported'),
69
    ('default_store=', 's', 'Default modulestore'),
70 71 72 73 74 75 76 77 78 79 80 81
    make_option("--verbose", action="store_const", const=2, dest="verbosity"),
    make_option("-q", "--quiet", action="store_const", const=0, dest="verbosity"),
    make_option("-v", "--verbosity", action="count", dest="verbosity"),
])
def perf_report_bokchoy(options):
    """
    Generates a har file for with page performance info.
    """
    opts = {
        'test_spec': getattr(options, 'test_spec', None),
        'fasttest': getattr(options, 'fasttest', False),
        'imports_dir': getattr(options, 'imports_dir', None),
82
        'default_store': getattr(options, 'default_store', None),
83 84 85
        'verbosity': getattr(options, 'verbosity', 2),
        'test_dir': 'performance',
        'ptests': True,
86
    }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    run_bokchoy(**opts)


def run_bokchoy(**opts):
    """
    Runs BokChoyTestSuite with the given options.
    If a default store is not specified, runs the test suite for 'split' as the default store.
    """
    if opts['default_store'] not in ['draft', 'split']:
        msg = colorize(
            'red',
            'No modulestore specified, running tests for split.'
        )
        print(msg)
        stores = ['split']
    else:
        stores = [opts['default_store']]
104

105 106 107 108
    for store in stores:
        opts['default_store'] = store
        test_suite = BokChoyTestSuite('bok-choy', **opts)
        test_suite.run()
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129


@task
def bokchoy_coverage():
    """
    Generate coverage reports for bok-choy tests
    """
    Env.BOK_CHOY_REPORT_DIR.makedirs_p()
    coveragerc = Env.BOK_CHOY_COVERAGERC

    msg = colorize('green', "Combining coverage reports")
    print(msg)

    sh("coverage combine --rcfile={}".format(coveragerc))

    msg = colorize('green', "Generating coverage reports")
    print(msg)

    sh("coverage html --rcfile={}".format(coveragerc))
    sh("coverage xml --rcfile={}".format(coveragerc))
    sh("coverage report --rcfile={}".format(coveragerc))