Skip to content

Commit

Permalink
Getting environment right to run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
engelke committed Nov 12, 2018
1 parent e512701 commit 39f58f8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .kokoro/common.cfg
Expand Up @@ -11,3 +11,9 @@ env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-kokoro-resources/python@sha256:4b6ba8c199e96248980db4538065cddeea594138b9b9fb2d0388603922087747"
}

# Specify project ID
env_vars: {
key: "GCLOUD_PROJECT"
value: "python-docs-samples"
}
38 changes: 38 additions & 0 deletions conftest.py
@@ -0,0 +1,38 @@
# 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.

import os

import mock
import pytest

PROJECT = os.environ['GCLOUD_PROJECT']


@pytest.fixture
def api_client_inject_project_id():
"""Patches all googleapiclient requests to replace 'YOUR_PROJECT_ID' with
the project ID."""
import googleapiclient.http

old_execute = googleapiclient.http.HttpRequest.execute

def new_execute(self, http=None, num_retries=0):
self.uri = self.uri.replace('YOUR_PROJECT_ID', PROJECT)
return old_execute(self, http=http, num_retries=num_retries)

with mock.patch(
'googleapiclient.http.HttpRequest.execute',
new=new_execute):
yield
24 changes: 18 additions & 6 deletions noxfile.py
@@ -1,4 +1,5 @@
from glob import glob
import os

import nox

Expand All @@ -18,6 +19,8 @@
'optional-kubernetes-engine'
]

PYTEST_COMMON_ARGS = ['--junitxml=sponge_log.xml']


@nox.session
def check_requirements(session):
Expand All @@ -40,16 +43,26 @@ def lint(session):
'--import-order-style=google', '.')


def run_test(session, dir, toxargs):
def run_test(session, dir):
session.install('-r', 'requirements.txt')
session.chdir(dir)
session.run('tox', *(toxargs or []))
if os.path.exists('requirements.txt'):
session.install('-r', 'requirements.txt')

session.run(
'pytest',
*(PYTEST_COMMON_ARGS + session.posargs),
# Pytest will return 5 when no tests are collected. This can happen
# on travis where slow and flaky tests are excluded.
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
success_codes=[0, 5])


@nox.session
@nox.parametrize('dir', DIRS)
def run_tests(session, dir=None, toxargs=None):
def run_tests(session, dir=None):
"""Run all tests for all directories (slow!)"""
run_test(session, dir, toxargs)
run_test(session, dir)


@nox.session
Expand All @@ -58,5 +71,4 @@ def travis(session, dir=None):
"""On travis, only run the py3.4 and cloudsql tests."""
run_tests(
session,
dir=dir,
toxargs=['-e', 'py34', '--', '-k', 'cloudsql'])
dir=dir)
6 changes: 6 additions & 0 deletions pytest.ini
@@ -0,0 +1,6 @@
[pytest]
addopts =
-v
--no-success-flaky-report
--tb=native
norecursedirs = .git env lib .tox .nox
8 changes: 8 additions & 0 deletions requirements.txt
@@ -0,0 +1,8 @@
BeautifulSoup4==4.6.0
flake8==3.5.0
flaky==3.4.0
mock==2.0.0
pytest==3.8.2
pytest-cov==2.5.1
retrying==1.3.3
requests==2.18.4

0 comments on commit 39f58f8

Please sign in to comment.