Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

Handle output in stderr gracefully when discovering tests #6

Merged
merged 6 commits into from
Jul 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,29 @@ packge. If you're using Python 2.7 on Ubuntu 13.04, you can install

For other versions of Python and Ubuntu, you'll need to adjust this as
appropriate.

Development
-----------

The recommended way of setting up your development envrionment for ``cricket``
is to install a virtual environment, install the required dependencies and
start coding. Assuming that you are using ``virtualenvwrapper``, you only have
to run::

$ git clone git@github.com:freakboy3742/cricket.git
$ cde cricket
$ mkvirtualenv cricket

Cricket uses ``unittest`` (or ``unittest2`` for Python < 2.7) for its own test
suite as well as additional helper modules for testing. To install all the
requirements for cricket, you have to run the following commands within your
virutal envrionment::

$ pip install -e .
$ pip install -r requirements_dev.txt

In case you are running a python version ``< 2.7`` please use the
``requirements_dev_python2.7.txt`` instead because ``unittest2`` is not part
of the standard library for these version.

Now you are ready to start hacking! Have fun!
13 changes: 13 additions & 0 deletions cricket/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is for backwards compatibility with Python version < 2.7 only
# for Python 2.7 and 3.x the default unittest is the correct package
# to use. unittest2 is a backport of this package to be used in
# versions < 2.7.
# Use
# from cricket.compat import unittest
#
# to make versions work with all version of Python. This will be slowly
# deprecated in the future as Python < 2.7 becomes more and mor
# obsolete.
import unittest
if not hasattr(unittest.TestCase, 'assertIsNotNone'):
import unittest2 as unittest
8 changes: 7 additions & 1 deletion cricket/django/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from Tkinter import *

from cricket.view import MainWindow, TestLoadErrorDialog
from cricket.view import (MainWindow, TestLoadErrorDialog,
IgnorableTestLoadErrorDialog)
from cricket.model import ModelLoadError
from cricket.django.model import DjangoProject

Expand All @@ -23,6 +24,11 @@ def main():
if dialog.status == dialog.CANCEL:
sys.exit(1)

if project.errors:
dialog = IgnorableTestLoadErrorDialog(root, '\n'.join(project.errors))
if dialog.status == dialog.CANCEL:
sys.exit(1)

# Set the project for the main window.
# This populates the tree, and sets listeners for
# future tree modifications.
Expand Down
9 changes: 4 additions & 5 deletions cricket/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ class Project(dict, EventSource):
"""
def __init__(self):
super(Project, self).__init__()

self.errors = []
self.discover_tests()

def __repr__(self):
Expand Down Expand Up @@ -458,12 +458,11 @@ def discover_tests(self):
for line in runner.stdout:
test_list.append(line.strip())

errors = []
for line in runner.stderr:
errors.append(line.strip())
self.errors.append(line.strip())

if errors:
raise ModelLoadError('\n'.join(errors))
if self.errors and not test_list:
raise ModelLoadError('\n'.join(self.errors))

self.refresh(test_list)

Expand Down
29 changes: 28 additions & 1 deletion cricket/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,35 @@ def __init__(self, parent, trace):
self,
parent,
'Error discovering test suite',
'The following stack trace was generated when attempting to discover the test suite:',
('The following stack trace was generated when attempting to '
'discover the test suite:'),
trace,
button_text='Retry',
cancel_text='Quit',
)

def cancel(self, event=None):
StackTraceDialog.cancel(self, event=event)
self.parent.quit()


class IgnorableTestLoadErrorDialog(StackTraceDialog):
def __init__(self, parent, trace):
'''Show a dialog with a scrollable stack trace when loading
tests turned up errors in stderr but they can safely be ignored.

Arguments:

parent -- a parent window (the application window)
trace -- the stack trace content to display.
'''
StackTraceDialog.__init__(
self,
parent,
'Error discovering test suite',
('The following error where captured during test discovery '
'but running the tests might still work:'),
trace,
button_text='Continue',
cancel_text='Quit',
)
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock==1.0.1
2 changes: 2 additions & 0 deletions requirements_dev_python2.6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r requirements_dev.txt
unittest2==0.5.1
51 changes: 51 additions & 0 deletions tests/model_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import mock

from cricket.compat import unittest
from cricket.model import Project, ModelLoadError


class TestProject(unittest.TestCase):

def setUp(self):
super(TestProject, self).setUp()
Project.discover_commandline = mock.MagicMock()

self.test_list = [
'tests.FunkyTestCase.test_something_unnecessary',
'more_tests.FunkyTestCase.test_this_doesnt_make_sense',
]
self.error_list = [
'ERROR: you broke it, fool!',
]

@mock.patch('cricket.model.subprocess.Popen')
def test_project_has_error_list_from_stderr(self, sub_mock):
io_mock = mock.MagicMock()
io_mock.stdout = self.test_list
io_mock.stderr = self.error_list
sub_mock.return_value = io_mock

project = Project()

self.assertEquals(project.errors, self.error_list)
self.assertItemsEqual(project.keys(), ['tests', 'more_tests'])

@mock.patch('cricket.model.subprocess.Popen')
def test_raises_exception_when_errors_no_tests(self, sub_mock):
io_mock = mock.MagicMock()
io_mock.stdout = []
io_mock.stderr = self.error_list
sub_mock.return_value = io_mock

self.assertRaises(ModelLoadError, Project)

@mock.patch('cricket.model.subprocess.Popen')
def test_finds_test_modules_without_errors(self, sub_mock):
io_mock = mock.MagicMock()
io_mock.stdout = self.test_list
io_mock.stderr = []
sub_mock.return_value = io_mock

project = Project()
self.assertEquals(project.errors, [])
self.assertItemsEqual(project.keys(), ['tests', 'more_tests'])