Skip to content

Commit

Permalink
fixes bug 908214 - replace self.assertRaises with assert_raises, r=Ad…
Browse files Browse the repository at this point in the history
…rianGaudebert
  • Loading branch information
peterbe committed Mar 13, 2014
1 parent e563981 commit 56978a2
Show file tree
Hide file tree
Showing 52 changed files with 265 additions and 257 deletions.
7 changes: 4 additions & 3 deletions docs/addaservice.rst
Expand Up @@ -193,6 +193,7 @@ Here is an example of an integration test file for a PostgreSQL service
(testing the service that was created in the previous section)::

from nose.plugins.attrib import attr
from nose.tools import eq_, assert_raises

from socorro.external import MissingOrBadArgumentError
from socorro.external.postgresql.crash import Crash
Expand Down Expand Up @@ -245,7 +246,7 @@ Here is an example of an integration test file for a PostgreSQL service
res_expected = {
'signature': 'fake_signature_1'
}
self.assertEqual(res, res_expected)
eq_(res, res_expected)

# Test 2: test something else
params = {
Expand All @@ -255,10 +256,10 @@ Here is an example of an integration test file for a PostgreSQL service
res_expected = {
'signature': 'fake_signature_3'
}
self.assertEqual(res, res_expected)
eq_(res, res_expected)

# Test 3: test the expections
self.assertRaises(
assert_raises(
MissingOrBadArgumentError,
api.get()
)
Expand Down
25 changes: 16 additions & 9 deletions docs/unittesting.rst
Expand Up @@ -291,12 +291,14 @@ Testing tools
There are many ways to verify if the results are what we originally
expected.

One of this forms is using convenience functions provided by unittest.
It includes all ``self.assertX`` methods of ``unittest.TestCase``::
The convention we use for writing tests is to use ``nose.tools``. For
example::

from nose.tools import eq_, ok_

self.assertFalse(expr, msg=None)
self.assertTrue(expr, msg=None)
self.assertEqual(first, second, msg=None)
ok_(not expr, msg=None)
ok_(expr, msg=None)
eq_(first, second, msg=None)

Also, we can use the Python's assert statement::

Expand All @@ -305,12 +307,15 @@ Also, we can use the Python's assert statement::
Exception tests try out if a function call raises a specified exception
when presented certain parameters::

self.assertRaises(nameOfException, functionCalled, *{arguments}, **{keywords})
from nose.tools import assert_raises

assert_raises(nameOfException, functionCalled, *{arguments}, **{keywords})

We could also want to write a test that fails but we don't want properly a
failure, so we skip that test showing a ``S`` while running the tests::

from nose.plugins.skip import SkipTest
from nose.tools import eq_

try:
eq_(line[0], 1)
Expand Down Expand Up @@ -389,9 +394,11 @@ exception while saving a broken processed crash::
"submitted_timestamp": time.time(),
"unknown_field": 'whatever'
}
self.assertRaises(KeyError,
crashstorage.save_processed,
broken_processed_crash)
assert_raises(
KeyError,
crashstorage.save_processed,
broken_processed_crash
)

Decorators
^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions socorro/unittest/app/test_generic_app.py
Expand Up @@ -7,7 +7,7 @@
import os
import unittest
import mock
from nose.tools import eq_
from nose.tools import eq_, assert_raises
from configman import Namespace
from configman.config_file_future_proxy import ConfigFileFutureProxy
from socorro.app.generic_app import App, main
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_overriding_config_path(self, logging):
eq_(exit_code, 0)

os.environ['DEFAULT_SOCORRO_CONFIG_PATH'] = '/foo/bar'
self.assertRaises(IOError, main, (MyApp,), values_source_list=vsl)
assert_raises(IOError, main, (MyApp,), values_source_list=vsl)

os.environ['DEFAULT_SOCORRO_CONFIG_PATH'] = self.tempdir
exit_code = main(MyApp, values_source_list=vsl)
Expand Down
6 changes: 3 additions & 3 deletions socorro/unittest/app/test_generic_fetch_transform_save_app.py
Expand Up @@ -4,7 +4,7 @@

import unittest

from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises

from socorro.app.fetch_transform_save_app import FetchTransformSaveApp
from socorro.lib.threaded_task_manager import ThreadedTaskManager
Expand Down Expand Up @@ -220,7 +220,7 @@ def save_raw_crash(self, raw_crash, dump, crash_id):

fts_app = FetchTransformSaveApp(config)

self.assertRaises(TypeError, fts_app.main)
assert_raises(TypeError, fts_app.main)

def test_no_destination(self):
class FakeStorageSource(object):
Expand Down Expand Up @@ -271,4 +271,4 @@ def new_ooids(self):

fts_app = FetchTransformSaveApp(config)

self.assertRaises(TypeError, fts_app.main)
assert_raises(TypeError, fts_app.main)
6 changes: 3 additions & 3 deletions socorro/unittest/collector/test_crash_mover_app.py
Expand Up @@ -4,7 +4,7 @@

import unittest

from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises

from socorro.collector.crashmover_app import CrashMoverApp
from socorro.lib.threaded_task_manager import ThreadedTaskManager
Expand Down Expand Up @@ -215,7 +215,7 @@ def save_raw_crash(self, raw_crash, dump, crash_id):

fts_app = CrashMoverApp(config)

self.assertRaises(TypeError, fts_app.main)
assert_raises(TypeError, fts_app.main)

def test_no_destination(self):
class FakeStorageSource(object):
Expand Down Expand Up @@ -267,4 +267,4 @@ def new_ooids(self):

fts_app = CrashMoverApp(config)

self.assertRaises(TypeError, fts_app.main)
assert_raises(TypeError, fts_app.main)
8 changes: 4 additions & 4 deletions socorro/unittest/collector/test_submitter_app.py
Expand Up @@ -7,7 +7,7 @@
import time
import json

from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises

from socorro.collector.submitter_app import (
SubmitterApp,
Expand Down Expand Up @@ -291,7 +291,7 @@ def test_source_iterator(self):
eq_(itera.next(), ((1,), {}))
eq_(itera.next(), ((2,), {}))
eq_(itera.next(), ((3,), {}))
self.assertRaises(StopIteration, itera.next)
assert_raises(StopIteration, itera.next)

# Test with number of submissions equal to forever
# It never raises StopIterations
Expand Down Expand Up @@ -328,7 +328,7 @@ def test_source_iterator(self):
eq_(itera.next(), ((3,), {}))
eq_(itera.next(), ((1,), {}))
eq_(itera.next(), ((2,), {}))
self.assertRaises(StopIteration, itera.next)
assert_raises(StopIteration, itera.next)

# Test with number of submissions equal to an integer < number of items
# It raises StopIterations after some number of elements were called
Expand All @@ -343,4 +343,4 @@ def test_source_iterator(self):
sub.source.new_crashes = mock.Mock(side_effect=sequence_generator)

eq_(itera.next(), ((1,), {}))
self.assertRaises(StopIteration, itera.next)
assert_raises(StopIteration, itera.next)
10 changes: 5 additions & 5 deletions socorro/unittest/cron/jobs/test_ftpscraper.py
Expand Up @@ -8,7 +8,7 @@
from cStringIO import StringIO
import mock
from nose.plugins.attrib import attr
from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises
from socorro.cron import crontabber
from socorro.lib.datetimeutil import utc_now
from socorro.cron.jobs import ftpscraper
Expand Down Expand Up @@ -115,7 +115,7 @@ def mocked_urlopener(url):

self.urllib2.side_effect = mocked_urlopener
# very impatient version
self.assertRaises(
assert_raises(
ftpscraper.RetriedError,
ftpscraper.patient_urlopen,
'http://doesntmatt.er',
Expand All @@ -125,7 +125,7 @@ def mocked_urlopener(url):

# less impatient
mock_calls = []
self.assertRaises(
assert_raises(
ftpscraper.RetriedError,
ftpscraper.patient_urlopen,
'http://doesntmatt.er',
Expand Down Expand Up @@ -154,7 +154,7 @@ def mocked_urlopener(url):

self.urllib2.side_effect = mocked_urlopener
# very impatient version
self.assertRaises(
assert_raises(
urllib2.HTTPError,
ftpscraper.patient_urlopen,
'http://doesntmatt.er',
Expand Down Expand Up @@ -195,7 +195,7 @@ def mocked_urlopener(url):

self.urllib2.side_effect = mocked_urlopener
# very impatient version
self.assertRaises(
assert_raises(
ftpscraper.RetriedError,
ftpscraper.patient_urlopen,
'http://doesntmatt.er',
Expand Down
18 changes: 9 additions & 9 deletions socorro/unittest/cron/test_crontabber.py
Expand Up @@ -13,7 +13,7 @@
import mock
import psycopg2
from nose.plugins.attrib import attr
from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises

from socorro.cron import crontabber
from socorro.cron import base
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_circular_no_roots(self):
_Item('B', ['A']),
_Item('A', ['C']),
]
self.assertRaises(
assert_raises(
base.CircularDAGError,
base.reorder_dag,
sequence
Expand All @@ -123,7 +123,7 @@ def test_circular_one_root_still_circular(self):
_Item('B', ['A']),
_Item('A', ['C']),
]
self.assertRaises(
assert_raises(
base.CircularDAGError,
base.reorder_dag,
sequence
Expand Down Expand Up @@ -309,7 +309,7 @@ def test_pop(self):
assert not self.database.has_data()
popped = self.database.pop('foo', 'default')
eq_(popped, 'default')
self.assertRaises(KeyError, self.database.pop, 'bar')
assert_raises(KeyError, self.database.pop, 'bar')


#==============================================================================
Expand Down Expand Up @@ -347,7 +347,7 @@ def fmt(d):
with config_manager.context() as config:
tab = crontabber.CronTabber(config)
config['job'] = 'unheard-of-app-name'
self.assertRaises(
assert_raises(
crontabber.JobNotFoundError,
tab.main,
)
Expand Down Expand Up @@ -540,7 +540,7 @@ def test_run_all_basic_with_failing_dependency_without_errors(self):
# hasn't never run
with config_manager.context() as config:
tab = crontabber.CronTabber(config)
self.assertRaises(
assert_raises(
base.CircularDAGError,
tab.run_all
)
Expand Down Expand Up @@ -745,14 +745,14 @@ def test_configtest_ok(self):
ok_(not new_stdout.getvalue())

def test_configtest_not_found(self):
self.assertRaises(
assert_raises(
crontabber.JobNotFoundError,
self._setup_config_manager,
'socorro.unittest.cron.test_crontabber.YYYYYY|3d'
)

def test_configtest_definition_error(self):
self.assertRaises(
assert_raises(
crontabber.JobDescriptionError,
self._setup_config_manager,
'socorro.unittest.cron.test_crontabber.FooJob'
Expand Down Expand Up @@ -1519,7 +1519,7 @@ def test_reset_job(self):
with config_manager.context() as config:
tab = crontabber.CronTabber(config)
config['reset-job'] = 'never-heard-of'
self.assertRaises(
assert_raises(
crontabber.JobNotFoundError,
tab.main,
)
Expand Down
6 changes: 3 additions & 3 deletions socorro/unittest/database/test_transaction_executor.py
Expand Up @@ -4,7 +4,7 @@

import unittest

from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises
import psycopg2
from configman import Namespace, ConfigurationManager, class_converter
import socorro.database.transaction_executor
Expand Down Expand Up @@ -150,7 +150,7 @@ def mock_function(connection):
psycopg2.extensions.TRANSACTION_STATUS_INTRANS
raise SomeError('crap!')

self.assertRaises(SomeError, executor, mock_function)
assert_raises(SomeError, executor, mock_function)

eq_(commit_count, 0)
eq_(rollback_count, 1)
Expand Down Expand Up @@ -394,6 +394,6 @@ def mock_function_developer_mistake(connection):
psycopg2.extensions.TRANSACTION_STATUS_INTRANS
raise psycopg2.ProgrammingError("syntax error")

self.assertRaises(psycopg2.ProgrammingError,
assert_raises(psycopg2.ProgrammingError,
executor,
mock_function_developer_mistake)
6 changes: 3 additions & 3 deletions socorro/unittest/external/elasticsearch/test_crashstorage.py
Expand Up @@ -6,7 +6,7 @@
import mock
import pyelasticsearch
from pyelasticsearch.exceptions import IndexAlreadyExistsError
from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises

from configman import ConfigurationManager

Expand Down Expand Up @@ -213,7 +213,7 @@ def test_failure_no_retry(self, pyes_mock):

crash_id = a_processed_crash['uuid']

self.assertRaises(
assert_raises(
Exception,
es_storage.save_raw_and_processed,
a_raw_crash,
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_failure_limited_retry(self, pyes_mock):

crash_id = a_processed_crash['uuid']

self.assertRaises(
assert_raises(
pyelasticsearch.exceptions.Timeout,
es_storage.save_raw_and_processed,
a_raw_crash,
Expand Down

0 comments on commit 56978a2

Please sign in to comment.