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

try:
    from pygments.console import colorize
except ImportError:
24
    colorize = lambda color, text: text
25 26 27

__test__ = False  # do not collect

28 29 30

@needs('pavelib.prereqs.install_prereqs')
@cmdopts(BOKCHOY_OPTS)
31
@PassthroughTask
32
@timed
33
def test_bokchoy(options, passthrough_options):
34 35
    """
    Run acceptance tests that use the bok-choy framework.
36 37 38 39
    Skips some static asset steps if `fasttest` is True.
    Using 'serversonly' will prepare and run servers, leaving a process running in the terminal. At
        the same time, a user can open a separate terminal and use 'testsonly' for executing tests against
        those running servers.
40 41 42 43 44 45 46 47

    `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.
    """
48 49 50 51 52 53
    # 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
54
        check_firefox_version()
55

56
    run_bokchoy(options.test_bokchoy, passthrough_options)
57 58 59 60


@needs('pavelib.prereqs.install_prereqs')
@cmdopts(BOKCHOY_OPTS)
61
@PassthroughTask
62
@timed
63
def test_a11y(options, passthrough_options):
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    """
    Run accessibility tests that use the bok-choy framework.
    Skips some static asset steps if `fasttest` is True.
    Using 'serversonly' will prepare and run servers, leaving a process running in the terminal. At
        the same time, a user can open a separate terminal and use 'testsonly' for executing tests against
        those running servers.

    `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 that are tagged
    with `@attr("a11y")`.
    """
79 80
    # Modify the options object directly, so that any subsequently called tasks
    # that share with this task get the modified options
81 82 83 84
    options.test_a11y.report_dir = Env.BOK_CHOY_A11Y_REPORT_DIR
    options.test_a11y.coveragerc = Env.BOK_CHOY_A11Y_COVERAGERC
    options.test_a11y.extra_args = options.get('extra_args', '') + ' -a "a11y" '
    run_bokchoy(options.test_a11y, passthrough_options)
85 86 87


@needs('pavelib.prereqs.install_prereqs')
88
@cmdopts(BOKCHOY_OPTS)
89
@PassthroughTask
90
@timed
91
def perf_report_bokchoy(options, passthrough_options):
92 93 94
    """
    Generates a har file for with page performance info.
    """
95 96
    # Modify the options object directly, so that any subsequently called tasks
    # that share with this task get the modified options
97
    options.perf_report_bokchoy.test_dir = 'performance'
98

99
    run_bokchoy(options.perf_report_bokchoy, passthrough_options)
100 101


102 103
@needs('pavelib.prereqs.install_prereqs', 'get_test_course')
@cmdopts(
104
    BOKCHOY_OPTS + [PA11Y_SINGLE_URL, PA11Y_HTML, PA11Y_COURSE_KEY, PA11Y_FETCH_COURSE],
105 106
    share_with=['get_test_course', 'prepare_bokchoy_run', 'load_courses']
)
107
@PassthroughTask
108
@timed
109
def pa11ycrawler(options, passthrough_options):
110 111 112 113 114 115 116 117
    """
    Runs pa11ycrawler against the demo-test-course to generates accessibility
    reports. (See https://github.com/edx/demo-test-course)

    Note: Like the bok-choy tests, this can be used with the `serversonly`
    flag to get an environment running. The setup for this is the same as
    for bok-choy tests, only test course is imported as well.
    """
118 119
    # Modify the options object directly, so that any subsequently called tasks
    # that share with this task get the modified options
120 121 122
    options.pa11ycrawler.report_dir = Env.PA11YCRAWLER_REPORT_DIR
    options.pa11ycrawler.coveragerc = Env.PA11YCRAWLER_COVERAGERC
    options.pa11ycrawler.should_fetch_course = getattr(
123 124 125 126
        options,
        'should_fetch_course',
        not options.get('fasttest')
    )
127
    options.pa11ycrawler.course_key = getattr(options, 'course-key', "course-v1:edX+Test101+course")
128
    test_suite = Pa11yCrawler('pa11ycrawler', passthrough_options=passthrough_options, **options.pa11ycrawler)
129 130 131 132 133 134
    test_suite.run()

    if getattr(options, 'with_html', False):
        test_suite.generate_html_reports()


135
def run_bokchoy(options, passthrough_options):
136 137 138
    """
    Runs BokChoyTestSuite with the given options.
    """
139
    test_suite = BokChoyTestSuite('bok-choy', passthrough_options=passthrough_options, **options)
140 141 142
    msg = colorize(
        'green',
        'Running tests using {default_store} modulestore.'.format(
143
            default_store=test_suite.default_store,
144
        )
145
    )
146
    print msg
147
    test_suite.run()
148 149


150
def parse_coverage(report_dir, coveragerc):
151
    """
152
    Generate coverage reports for bok-choy or a11y tests
153
    """
154
    report_dir.makedirs_p()
155 156

    msg = colorize('green', "Combining coverage reports")
157
    print msg
158 159 160 161

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

    msg = colorize('green', "Generating coverage reports")
162
    print msg
163 164 165 166

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


@task
170
@timed
171 172 173 174 175 176 177 178 179 180 181
def bokchoy_coverage():
    """
    Generate coverage reports for bok-choy tests
    """
    parse_coverage(
        Env.BOK_CHOY_REPORT_DIR,
        Env.BOK_CHOY_COVERAGERC
    )


@task
182
@timed
183 184 185 186 187 188 189 190 191 192 193 194
def a11y_coverage():
    """
    Generate coverage reports for a11y tests. Note that this coverage report
    is just a guideline to find areas that are missing tests.  If the view
    isn't 'covered', there definitely isn't a test for it.  If it is
    'covered', we are loading that page during the tests but not necessarily
    calling ``page.a11y_audit.check_for_accessibility_errors`` on it.
    """
    parse_coverage(
        Env.BOK_CHOY_A11Y_REPORT_DIR,
        Env.BOK_CHOY_A11Y_COVERAGERC
    )
195 196 197


@task
198
@timed
199 200 201 202 203 204 205 206
def pa11ycrawler_coverage():
    """
    Generate coverage reports for bok-choy tests
    """
    parse_coverage(
        Env.PA11YCRAWLER_REPORT_DIR,
        Env.PA11YCRAWLER_COVERAGERC
    )