Exemplo n.º 1
0
def test_popen_streaming_output_timeout():
    start = time()
    with pytest.raises(TimeoutError):
        popen_streaming_output(
            PYTHON + ' -c "import time; time.sleep(4)"',
            lambda line: line, timeout=0.1,
        )

    assert (time() - start) < 3
Exemplo n.º 2
0
def test_popen_streaming_output_stream():
    mock = MagicMock()
    popen_streaming_output(PYTHON +
                           ' -c "print(\'first\'); print(\'second\')"',
                           callback=mock)
    if os.name == 'nt':
        mock.assert_has_calls([call('first\r\n'), call('second\r\n')])
    else:
        mock.assert_has_calls([call('first\n'), call('second\n')])

    mock = MagicMock()
    popen_streaming_output(
        PYTHON +
        ' -c "import time; print(\'first\'); print(\'second\'); print(\'third\')"',
        callback=mock)
    if os.name == 'nt':
        mock.assert_has_calls(
            [call('first\r\n'),
             call('second\r\n'),
             call('third\r\n')])
    else:
        mock.assert_has_calls(
            [call('first\n'),
             call('second\n'),
             call('third\n')])

    mock = MagicMock()
    popen_streaming_output(PYTHON + ' -c "exit(0);"', callback=mock)
    mock.assert_not_called()
Exemplo n.º 3
0
def time_test_suite(swallow_output, test_command, using_testmon,
                    current_hash_of_tests):
    """Execute a test suite specified by ``test_command`` and record
    the time it took to execute the test suite as a floating point number

    :param swallow_output: if :obj:`True` test stdout will be not be printed
    :type swallow_output: bool

    :param test_command: command to spawn the testing subprocess
    :type test_command: str

    :param using_testmon: if :obj:`True` the test return code evaluation will
        accommodate for ``pytest-testmon``
    :type using_testmon: bool

    :return: execution time of the test suite
    :rtype: float
    """
    cached_time = cached_test_time()
    if cached_time is not None and current_hash_of_tests == cached_hash_of_tests(
    ):
        print(
            '1. Using cached time for baseline tests, to run baseline again delete the cache file'
        )
        return cached_time

    print('1. Running tests without mutations')
    start_time = time()

    output = []

    def feedback(line):
        if not swallow_output:
            print(line)
        print_status('Running...')
        output.append(line)

    returncode = popen_streaming_output(test_command, feedback)

    if returncode == 0 or (using_testmon and returncode == 5):
        baseline_time_elapsed = time() - start_time
    else:
        raise RuntimeError(
            "Tests don't run cleanly without mutations. Test command was: {}\n\nOutput:\n\n{}"
            .format(test_command, '\n'.join(output)))

    print('Done')

    set_cached_test_time(baseline_time_elapsed, current_hash_of_tests)

    return baseline_time_elapsed