Skip to content

Commit

Permalink
Bug 1361764 - initial switch to pytest (mozilla-services#3787)
Browse files Browse the repository at this point in the history
* Bug 1361764 - initial switch to pytest

This switches the test runner from nose to pytest for socorro/unittest/
tests (i.e. not the webapp tests).

This doesn't get rid of nose completely--we still have to go through and remove
ok_ and eq_ instances. You can continue to use nose if you like.

This fixes tests that were broken in pytest so that they work with both pytest
and nose.

* Fix env vars for local running

* Fixed tests when ran locally, without any env variables.

* Now using pytest-env instead.
  • Loading branch information
willkg authored May 23, 2017
1 parent 47a2f52 commit 9ff1852
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 79 deletions.
4 changes: 2 additions & 2 deletions docker/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ DATABASE_URL=${database_url:-"postgres://postgres:aPassword@postgresql:5432/soco
ELASTICSEARCH_URL=${elasticsearch_url:-"http://elasticsearch:9200"}

export PYTHONPATH=/app/:$PYTHONPATH
NOSE="$(which nosetests)"
PYTEST="$(which pytest)"
ALEMBIC="$(which alembic)"
SETUPDB="$(which python) /app/socorro/external/postgresql/setupdb_app.py"

Expand Down Expand Up @@ -77,7 +77,7 @@ $ALEMBIC -c "${alembic_config}" downgrade -1
$ALEMBIC -c "${alembic_config}" upgrade heads

# Run tests
$NOSE socorro -s
$PYTEST

# Collect static and then test webapp
pushd webapp-django
Expand Down
11 changes: 11 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,14 @@ django_csp==3.1 \
urlwait==0.4 \
--hash=sha256:fc39ff2c8abbcaad5043e1f79699dcb15a036cc4b0ff4d1aa825ea105d4889ff \
--hash=sha256:395fc0c2a7f9736858a2c2f449aa20c6e9da1f86bfc2d1fda4f2f5b78a5c115a
py==1.4.33 \
--hash=sha256:81b5e37db3cc1052de438375605fb5d3b3e97f950f415f9143f04697c684d7eb \
--hash=sha256:1f9a981438f2acc20470b301a07a496375641f902320f70e31916fe3377385a9
pytest==3.0.7 \
--hash=sha256:66f332ae62593b874a648b10a8cb106bfdacd2c6288ed7dec3713c3a808a6017 \
--hash=sha256:b70696ebd1a5e6b627e7e3ac1365a4bc60aaf3495e843c1e70448966c5224cab
pytest-wholenodeid==0.2 \
--hash=sha256:6bc4edaf560c8b45ac4669bb8b99d8fbbb7f6e5af8dc7727200cd4d3ef4668c2 \
--hash=sha256:bdcb4df88ca182a19752ef95150c0881deaafc2c593ef67c61515894e5309206
pytest-env==0.6.0 \
--hash=sha256:f4d61885b0befcb474e73e775597a798bd37808f8856ebb624b0822f054f6c0c
4 changes: 2 additions & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ echo "this is test.sh"

source scripts/defaults

NOSE="$VIRTUAL_ENV/bin/nosetests socorro -s"
PYTEST="$VIRTUAL_ENV/bin/pytest"
SETUPDB="$VIRTUAL_ENV/bin/python ./socorro/external/postgresql/setupdb_app.py"
JENKINS_CONF=jenkins.py.dist

Expand Down Expand Up @@ -100,7 +100,7 @@ PYTHONPATH=$PYTHONPATH ${VIRTUAL_ENV}/bin/alembic -c config/alembic.ini downgrad
PYTHONPATH=$PYTHONPATH ${VIRTUAL_ENV}/bin/alembic -c config/alembic.ini upgrade heads

# run tests
$ENV $FS_RESOURCES $PG_RESOURCES $RMQ_RESOURCES $ES_RESOURCES PYTHONPATH=$PYTHONPATH $NOSE
$ENV $FS_RESOURCES $PG_RESOURCES $RMQ_RESOURCES $ES_RESOURCES PYTHONPATH=$PYTHONPATH $PYTEST

# test webapp
pushd webapp-django
Expand Down
11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@
ignore = E265
exclude=webapp-django/crashstats/*/migrations/*,docs/conf.py,socorro/unittest/testlib/*
max-line-length = 100

[tool:pytest]
addopts = -rsxX --tb=native
norecursedirs = .git docs scripts config docker
testpaths = socorro/unittest/
env =
D:database_hostname=localhost
D:database_username=breakpad_rw
D:database_password=aPassword
D:resource.fs.fs_root='./crashes'
D:resource.elasticsearch.elasticsearch_urls=http://localhost:9200
54 changes: 27 additions & 27 deletions socorro/unittest/external/statsd/test_stastsd_rule_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
)
from socorro.external.statsd.statsd_base import StatsdCounter
from socorro.unittest.lib.test_transform_rules import (
TestRuleTestLaughable,
TestRuleTestDangerous
RuleTestLaughable,
RuleTestDangerous
)
from socorro.lib import transform_rules
from socorro.external.statsd.dogstatsd import StatsClient
Expand All @@ -36,33 +36,33 @@ def setup_config(self, prefix=None):
config.rules_list = DotDict()
config.rules_list.class_list = [
(
'TestRuleTestLaughable',
'RuleTestLaughable',
StatsdRuleBenchmarkWrapper,
'TestRuleTestLaughable'
'RuleTestLaughable'
),
(
'TestRuleTestDangerous',
'RuleTestDangerous',
StatsdRuleBenchmarkWrapper,
'TestRuleTestDangerous'
'RuleTestDangerous'
)
]
config.TestRuleTestLaughable = DotDict()
config.TestRuleTestLaughable.laughable = 'wilma'
config.TestRuleTestLaughable.statsd_class = StatsClient
config.TestRuleTestLaughable.statsd_host = 'some_statsd_host'
config.TestRuleTestLaughable.statsd_port = 3333
config.TestRuleTestLaughable.statsd_prefix = prefix if prefix else ''
config.TestRuleTestLaughable.wrapped_object_class = TestRuleTestLaughable
config.TestRuleTestLaughable.active_list = 'act'

config.TestRuleTestDangerous = DotDict()
config.TestRuleTestDangerous.dangerous = 'dwight'
config.TestRuleTestDangerous.statsd_class = StatsClient
config.TestRuleTestDangerous.statsd_host = 'some_statsd_host'
config.TestRuleTestDangerous.statsd_port = 3333
config.TestRuleTestDangerous.statsd_prefix = prefix if prefix else ''
config.TestRuleTestDangerous.wrapped_object_class = TestRuleTestDangerous
config.TestRuleTestDangerous.active_list = 'act'
config.RuleTestLaughable = DotDict()
config.RuleTestLaughable.laughable = 'wilma'
config.RuleTestLaughable.statsd_class = StatsClient
config.RuleTestLaughable.statsd_host = 'some_statsd_host'
config.RuleTestLaughable.statsd_port = 3333
config.RuleTestLaughable.statsd_prefix = prefix if prefix else ''
config.RuleTestLaughable.wrapped_object_class = RuleTestLaughable
config.RuleTestLaughable.active_list = 'act'

config.RuleTestDangerous = DotDict()
config.RuleTestDangerous.dangerous = 'dwight'
config.RuleTestDangerous.statsd_class = StatsClient
config.RuleTestDangerous.statsd_host = 'some_statsd_host'
config.RuleTestDangerous.statsd_port = 3333
config.RuleTestDangerous.statsd_prefix = prefix if prefix else ''
config.RuleTestDangerous.wrapped_object_class = RuleTestDangerous
config.RuleTestDangerous.active_list = 'act'

return config

Expand All @@ -72,9 +72,9 @@ def test_apply_all(self, statsd_obj):
trs = transform_rules.TransformRuleSystem(config)

ok_(isinstance(trs.rules[0], StatsdRuleBenchmarkWrapper))
ok_(isinstance(trs.rules[0].wrapped_object, TestRuleTestLaughable))
ok_(isinstance(trs.rules[0].wrapped_object, RuleTestLaughable))
ok_(isinstance(trs.rules[1], StatsdRuleBenchmarkWrapper))
ok_(isinstance(trs.rules[1].wrapped_object, TestRuleTestDangerous))
ok_(isinstance(trs.rules[1].wrapped_object, RuleTestDangerous))

now_str = 'socorro.external.statsd.statsd_base.datetime'
with patch(now_str) as now_mock:
Expand All @@ -92,11 +92,11 @@ def test_apply_all(self, statsd_obj):
trs.apply_all_rules()
statsd_obj.timing.has_calls([
call(
'timing.TestRuleTestLaughable.act',
'timing.RuleTestLaughable.act',
1000 # 1 second
),
call(
'timing.TestRuleTestDangerous.act',
'timing.RuleTestDangerous.act',
1000 # 1 second
),
])
Expand Down
File renamed without changes.
File renamed without changes.
72 changes: 37 additions & 35 deletions socorro/unittest/lib/test_transform_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ def assert_expected(actual, expected):
assert actual == expected, "expected:\n%s\nbut got:\n%s" % (str(expected),
str(actual))


def assert_expected_same(actual, expected):
assert actual == expected, "expected:\n%s\nbut got:\n%s" % (expected,
actual)


def foo(s, d):
pass

Expand All @@ -30,7 +32,7 @@ def bar(s, d):
pass


class TestRuleTestLaughable(transform_rules.Rule):
class RuleTestLaughable(transform_rules.Rule):
required_config = Namespace()
required_config.add_option('laughable', default='fred')

Expand All @@ -44,7 +46,7 @@ def close(self):
self.close_counter = 1


class TestRuleTestDangerous(transform_rules.Rule):
class RuleTestDangerous(transform_rules.Rule):
required_config = Namespace()
required_config.add_option('dangerous', default='sally')

Expand All @@ -58,13 +60,13 @@ def close(self):
self.close_counter = 1


class TestRuleTestNoCloseMethod(transform_rules.Rule):
class RuleTestNoCloseMethod(transform_rules.Rule):

def _action(self, *args, **kwargs):
return True


class TestRuleTestBrokenCloseMethod(transform_rules.Rule):
class RuleTestBrokenCloseMethod(transform_rules.Rule):

def _action(self, *args, **kwargs):
return true
Expand Down Expand Up @@ -704,25 +706,25 @@ def test_rules_in_config(self):
config.chatty = False
config.tag = 'test.rule'
config.action = 'apply_all_rules'
config['TestRuleTestLaughable.laughable'] = 'wilma'
config['TestRuleTestDangerous.dangerous'] = 'dwight'
config['RuleTestLaughable.laughable'] = 'wilma'
config['RuleTestDangerous.dangerous'] = 'dwight'
config.rules_list = DotDict()
config.rules_list.class_list = [
(
'TestRuleTestLaughable',
TestRuleTestLaughable,
'TestRuleTestLaughable'
'RuleTestLaughable',
RuleTestLaughable,
'RuleTestLaughable'
),
(
'TestRuleTestDangerous',
TestRuleTestDangerous,
'TestRuleTestDangerous'
'RuleTestDangerous',
RuleTestDangerous,
'RuleTestDangerous'
)
]
trs = transform_rules.TransformRuleSystem(config)

ok_(isinstance(trs.rules[0], TestRuleTestLaughable))
ok_(isinstance(trs.rules[1], TestRuleTestDangerous))
ok_(isinstance(trs.rules[0], RuleTestLaughable))
ok_(isinstance(trs.rules[1], RuleTestDangerous))
ok_(trs.rules[0].predicate(None))
ok_(trs.rules[1].action(None))

Expand All @@ -733,19 +735,19 @@ def test_rules_close(self):
config.chatty = False
config.tag = 'test.rule'
config.action = 'apply_all_rules'
config['TestRuleTestLaughable.laughable'] = 'wilma'
config['TestRuleTestDangerous.dangerous'] = 'dwight'
config['RuleTestLaughable.laughable'] = 'wilma'
config['RuleTestDangerous.dangerous'] = 'dwight'
config.rules_list = DotDict()
config.rules_list.class_list = [
(
'TestRuleTestLaughable',
TestRuleTestLaughable,
'TestRuleTestLaughable'
'RuleTestLaughable',
RuleTestLaughable,
'RuleTestLaughable'
),
(
'TestRuleTestDangerous',
TestRuleTestDangerous,
'TestRuleTestDangerous'
'RuleTestDangerous',
RuleTestDangerous,
'RuleTestDangerous'
)
]
trs = transform_rules.TransformRuleSystem(config)
Expand All @@ -765,14 +767,14 @@ def test_rules_close_if_close_method_available(self):
config.rules_list = DotDict()
config.rules_list.class_list = [
(
'TestRuleTestNoCloseMethod',
TestRuleTestNoCloseMethod,
'TestRuleTestNoCloseMethod'
'RuleTestNoCloseMethod',
RuleTestNoCloseMethod,
'RuleTestNoCloseMethod'
),
(
'TestRuleTestDangerous',
TestRuleTestDangerous,
'TestRuleTestDangerous'
'RuleTestDangerous',
RuleTestDangerous,
'RuleTestDangerous'
)
]
trs = transform_rules.TransformRuleSystem(config)
Expand All @@ -782,17 +784,17 @@ def test_rules_close_if_close_method_available(self):
config.logger.debug.assert_any_call(
'trying to close %s',
'socorro.unittest.lib.test_transform_rules.'
'TestRuleTestNoCloseMethod'
'RuleTestNoCloseMethod'
)
config.logger.debug.assert_any_call(
'trying to close %s',
'socorro.unittest.lib.test_transform_rules.'
'TestRuleTestDangerous'
'RuleTestDangerous'
)
config.logger.debug.assert_any_call(
'%s has no close',
'socorro.unittest.lib.test_transform_rules.'
'TestRuleTestNoCloseMethod'
'RuleTestNoCloseMethod'
)

def test_rules_close_bubble_close_errors(self):
Expand All @@ -803,9 +805,9 @@ def test_rules_close_bubble_close_errors(self):
config.rules_list = DotDict()
config.rules_list.class_list = [
(
'TestRuleTestBrokenCloseMethod',
TestRuleTestBrokenCloseMethod,
'TestRuleTestBrokenCloseMethod'
'RuleTestBrokenCloseMethod',
RuleTestBrokenCloseMethod,
'RuleTestBrokenCloseMethod'
),
]
trs = transform_rules.TransformRuleSystem(config)
Expand All @@ -818,5 +820,5 @@ def test_rules_close_bubble_close_errors(self):
config.logger.debug.assert_any_call(
'trying to close %s',
'socorro.unittest.lib.test_transform_rules.'
'TestRuleTestBrokenCloseMethod'
'RuleTestBrokenCloseMethod'
)
File renamed without changes.
Loading

0 comments on commit 9ff1852

Please sign in to comment.