Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Migrate test runner to py.test #569

Merged
merged 2 commits into from
Aug 10, 2016
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
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[report]
omit =
*/samples/*
*/conftest.py
# Don't report coverage over platform-specific modules.
oauth2client/contrib/_fcntl_opener.py
oauth2client/contrib/_win32_opener.py
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ docs/_build

# Test files
.tox/
.cache/

# Django test database
db.sqlite3
Expand Down
22 changes: 0 additions & 22 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +0,0 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test package set-up."""

from oauth2client import _helpers

__author__ = '[email protected] (Ali Afshar)'


def setup_package():
"""Run on testing package."""
_helpers.positional_parameters_enforcement = _helpers.POSITIONAL_EXCEPTION
31 changes: 31 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Py.test hooks."""

from oauth2client import _helpers


def pytest_addoption(parser):
"""Adds the --gae-sdk option to py.test.
This is used to enable the GAE tests. This has to be in this conftest.py
due to the way py.test collects conftest files."""
parser.addoption('--gae-sdk')


def pytest_configure(config):
"""Py.test hook called before loading tests."""
# Default of POSITIONAL_WARNING is too verbose for testing
_helpers.positional_parameters_enforcement = _helpers.POSITIONAL_EXCEPTION
Empty file.
53 changes: 53 additions & 0 deletions tests/contrib/appengine/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""App Engine py.test configuration."""

import sys

from six.moves import reload_module


def set_up_gae_environment(sdk_path):
"""Set up appengine SDK third-party imports.
The App Engine SDK does terrible things to the global interpreter state.
Because of this, this stuff can't be neatly undone. As such, it can't be
a fixture.
"""
if 'google' in sys.modules:
# Some packages, such as protobuf, clobber the google
# namespace package. This prevents that.
reload_module(sys.modules['google'])

# This sets up google-provided libraries.
sys.path.insert(0, sdk_path)
import dev_appserver
dev_appserver.fix_sys_path()

# Fixes timezone and other os-level items.
import google.appengine.tools.os_compat # noqa: unused import


def pytest_configure(config):
"""Configures the App Engine SDK imports on py.test startup."""
if config.getoption('gae_sdk') is not None:
set_up_gae_environment(config.getoption('gae_sdk'))


def pytest_ignore_collect(path, config):

This comment was marked as spam.

"""Skip App Engine tests when --gae-sdk is not specified."""
return (
'contrib/appengine' in str(path) and
config.getoption('gae_sdk') is None)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from oauth2client.contrib import appengine


DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data')
DATA_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'data')


def datafile(filename):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
import tempfile
import time

import dev_appserver

dev_appserver.fix_sys_path()

from google.appengine.api import apiproxy_stub
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import app_identity
Expand All @@ -41,11 +37,11 @@
from oauth2client import client
from oauth2client import clientsecrets
from oauth2client.contrib import appengine
from .. import http_mock
from ... import http_mock

__author__ = '[email protected] (Joe Gregorio)'

DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data')
DATA_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'data')
DEFAULT_RESP = """\
{
"access_token": "foo_access_token",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,13 @@ def setUp(self):
class SignedJwtAssertionCredentialsTests(unittest2.TestCase):

def setUp(self):
self.orig_signer = crypt.Signer
self.format_ = 'p12'
crypt.Signer = crypt.OpenSSLSigner

def tearDown(self):
crypt.Signer = self.orig_signer

def _make_credentials(self):
private_key = datafile('privatekey.' + self.format_)
signer = crypt.Signer.from_string(private_key)
Expand Down Expand Up @@ -310,17 +314,25 @@ class PEMSignedJwtAssertionCredentialsOpenSSLTests(
SignedJwtAssertionCredentialsTests):

def setUp(self):
self.orig_signer = crypt.Signer
self.format_ = 'pem'
crypt.Signer = crypt.OpenSSLSigner

def tearDown(self):
crypt.Signer = self.orig_signer


class PEMSignedJwtAssertionCredentialsPyCryptoTests(
SignedJwtAssertionCredentialsTests):

def setUp(self):
self.orig_signer = crypt.Signer
self.format_ = 'pem'
crypt.Signer = crypt.PyCryptoSigner

def tearDown(self):
crypt.Signer = self.orig_signer


class TestHasOpenSSLFlag(unittest2.TestCase):

Expand Down
6 changes: 6 additions & 0 deletions tests/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def datafile(filename):
class ServiceAccountCredentialsTests(unittest2.TestCase):

def setUp(self):
self.orig_signer = crypt.Signer
self.orig_verifier = crypt.Verifier
self.client_id = '123'
self.service_account_email = '[email protected]'
self.private_key_id = 'ABCDEF'
Expand All @@ -58,6 +60,10 @@ def setUp(self):
client_id=self.client_id,
)

def tearDown(self):
crypt.Signer = self.orig_signer
crypt.Verifier = self.orig_verifier

def test__to_json_override(self):
signer = object()
creds = service_account.ServiceAccountCredentials(
Expand Down
67 changes: 19 additions & 48 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ basedeps = mock>=1.3.0
cryptography>=1.0
pyopenssl>=0.14
webtest
nose
pytest
flask
unittest2
sqlalchemy
Expand All @@ -18,60 +18,41 @@ deps = {[testenv]basedeps}
setenv =
pypy: with_gmp=no
DJANGO_SETTINGS_MODULE=tests.contrib.django_util.settings
commands = nosetests --ignore-files=test_appengine\.py --ignore-files=test__appengine_ndb\.py {posargs}
commands =
py.test {posargs}

[coverbase]
basepython = python2.7
commands =
nosetests \
--with-coverage \
--cover-package=oauth2client \
--cover-package=tests \
--cover-erase \
--cover-tests \
--cover-branches \
--ignore-files=test_appengine\.py \
--ignore-files=test__appengine_ndb\.py
nosetests \
--with-coverage \
--cover-package=oauth2client.contrib.appengine \
--cover-package=oauth2client.contrib._appengine_ndb \
--cover-package=tests.contrib.test_appengine \
--cover-package=tests.contrib.test__appengine_ndb \
--with-gae \
--cover-tests \
--cover-branches \
--gae-application=tests/data \
--gae-lib-root={env:GAE_PYTHONPATH:google_appengine} \
--logging-level=INFO \
tests/contrib/test_appengine.py \
tests/contrib/test__appengine_ndb.py
py.test \
--cov=oauth2client \
--cov=tests
py.test \
--cov=oauth2client \
--cov=tests \
--cov-append \
--gae-sdk={env:GAE_PYTHONPATH} \
tests/contrib/appengine
deps = {[testenv]deps}
coverage
nosegae
pytest-cov

[testenv:py33]
basepython =
python3.3
commands =
nosetests \
--ignore-files=test_appengine\.py \
--ignore-files=test__appengine_ndb\.py \
--ignore-files=test_django_orm\.py \
--ignore-files=test_django_settings\.py \
--ignore-files=test_django_util\.py \
--exclude-dir=oauth2client/contrib/django_util \
--exclude-dir=tests/contrib/django_util \
py.test \
--ignore=oauth2client/contrib/django_util \
--ignore=tests/contrib/django_util \
{posargs}
deps = {[testenv]basedeps}
keyring
nose-exclude

[testenv:cover]
basepython = {[coverbase]basepython}
commands =
{[coverbase]commands}
coverage report --show-missing --cover-min-percentage=100
coverage report --show-missing --fail-under=100
deps =
{[coverbase]deps}

Expand Down Expand Up @@ -100,15 +81,8 @@ commands = {toxinidir}/scripts/build_docs.sh
[testenv:gae]
basepython = python2.7
deps = {[testenv]basedeps}
nosegae
commands =
nosetests \
--with-gae \
--gae-lib-root={env:GAE_PYTHONPATH:google_appengine} \
--gae-application=tests/data \
--logging-level=INFO \
tests/contrib/test_appengine.py \
tests/contrib/test__appengine_ndb.py
py.test --gae-sdk={env:GAE_PYTHONPATH} tests/contrib/appengine

[testenv:system-tests]
basepython =
Expand Down Expand Up @@ -153,11 +127,8 @@ exclude = .tox,.git,./*.egg,build,
application-import-names = oauth2client
putty-ignore =
# E402 module level import not at top of file
# These files have needed configurations defined before import
# This file has needed configurations defined before import
docs/conf.py : E402
tests/contrib/test_appengine.py : E402
# Additionally, ignore E100 (imports in wrong order) for Django configuration
tests/contrib/test_django_orm.py : E402,I100
# E501 line too long
# Ignore lines over 80 chars that include "http:" or "https:"
/http:/ : E501
Expand Down