Skip to content

Commit e2edf79

Browse files
authored
Adding code coverage (#9)
* Adding code coverage * Formatting changes; New test case for invalid cert * Minor doc update * Added pypy to the list of tested environments; Updated docs on using pyenv for testing * Removing some redundant empty lines * Updated doc
1 parent dd99cf7 commit e2edf79

File tree

7 files changed

+86
-15
lines changed

7 files changed

+86
-15
lines changed

.github/CONTRIBUTING.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ pytest
134134
Refer to the pytest [usage and invocations guide](http://doc.pytest.org/en/latest/usage.html)
135135
to learn how to run a subset of all test cases.
136136

137+
You can also get a code coverage report by launching pytest as follows:
138+
139+
```
140+
pytest --cov=firebase_admin --cov=tests
141+
```
142+
137143
### Testing in Different Environments
138144

139145
Sometimes we want to run unit tests in multiple environments (e.g. different Python versions), and
@@ -149,20 +155,40 @@ configuration file such as `.bash_profile`. Once pyenv is installed, you can ins
149155
versions of Python as follows:
150156

151157
```
152-
pyenv install 2.7.6 # install Python 2.7.6
153-
pyenv install 3.3.0 # install Python 3.3.0
158+
pyenv install 2.7.6 # install Python 2.7.6
159+
pyenv install 3.3.0 # install Python 3.3.0
160+
pyenv install pypy2-5.6.0 # install pypy2
154161
```
155162

156163
Refer to the [`tox.ini`](../tox.ini) file for a list of target environments that we usually test.
157-
Use pyenv to install all the required Python versions on your workstation. When your system is
158-
fully set up, you can execute the following command from the root of the repository to launch tox:
164+
Use pyenv to install all the required Python versions on your workstation. Verify that they are
165+
installed by running the following command:
166+
167+
```
168+
pyenv versions
169+
```
170+
171+
To make all the required Python versions available to tox for testing, run the `pyenv local` command
172+
with all the Python versions as arguments. The following example shows how to make Python versions
173+
2.7.6, 3.3.0 and pypy2 available to tox.
174+
175+
```
176+
pyenv local 2.7.6 3.3.0 pypy2-5.6.0
177+
```
178+
179+
Once your system is fully set up, you can execute the following command from the root of the
180+
repository to launch tox:
159181

160182
```
161183
tox
162184
```
163185

164186
This command will read the list of target environments from `tox.ini`, and execute tests in each of
165-
those environments.
187+
those environments. It will also generate a code coverage report at the end of the execution.
188+
189+
```
190+
pyenv local 2.7.6 3.3.0 pypy2-5.6.0
191+
```
166192

167193
### Repo Organization
168194

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.coverage
12
*.pyc
23
.python-version
34
.cache/

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pylint >= 1.6.4
22
pytest >= 3.0.6
3+
pytest-cov >= 2.4.0
34
tox >= 2.6.0
45

56
oauth2client >= 4.0.0

tests/data/malformed_key.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "mock-project-id",
4+
"private_key_id": "mock-key-id-1",
5+
"private_key": "-----INVALID PRIVATE KEY-----",
6+
"client_email": "[email protected]",
7+
"client_id": "1234567890",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://accounts.google.com/o/oauth2/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/mock-project-id.iam.gserviceaccount.com"
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "mock-project-id",
4+
"private_key_id": "mock-key-id-1",
5+
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAwJENcRev+eXZKvhhWLiV3Lz2MvO+naQRHo59g3vaNQnbgyduN/L4krlr\nJ5c6FiikXdtJNb/QrsAHSyJWCu8j3T9CruiwbidGAk2W0RuViTVspjHUTsIHExx9euWM0Uom\nGvYkoqXahdhPL/zViVSJt+Rt8bHLsMvpb8RquTIb9iKY3SMV2tCofNmyCSgVbghq/y7lKORt\nV/IRguWs6R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lnFGTFnRjej86hiw\nH3jPxBhHra0a53EgiilmsBGSnWpl1WH4EmJz5vBCKUAmjgQiBrueIqv9iHiaTNdjsanUyaWw\njyxXfXl2eI80QPXh02+8g1H/pzESgjK7Rg1AqnkfVH9nrwKBgQDJVxKBPTw9pigYMVt9iHrR\niCl9zQVjRMbWiPOc0J56+/5FZYm/AOGl9rfhQ9vGxXZYZiOP5FsNkwt05Y1UoAAH4B4VQwbL\nqod71qOcI0ywgZiIR87CYw40gzRfjWnN+YEEW1qfyoNLilEwJB8iB/T+ZePHGmJ4MmQ/cTn9\nxpdLXA==\n-----END RSA PRIVATE KEY-----",
6+
"client_email": "[email protected]",
7+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
8+
"token_uri": "https://accounts.google.com/o/oauth2/token",
9+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
10+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/mock-project-id.iam.gserviceaccount.com"
11+
}

tests/test_credentials.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
class TestCertificate(object):
1515

16+
invalid_certs = {
17+
'NonExistingFile': ('non_existing.json', IOError),
18+
'RefreskToken': ('refresh_token.json', ValueError),
19+
'MalformedPrivateKey': ('malformed_key.json', ValueError),
20+
'MissingClientId': ('no_client_id_service_account.json', ValueError),
21+
}
22+
1623
def test_init_from_file(self):
1724
credential = credentials.Certificate(
1825
testutils.resource_filename('service_account.json'))
@@ -30,15 +37,10 @@ def test_init_from_file(self):
3037
assert isinstance(access_token.access_token, six.string_types)
3138
assert isinstance(access_token.expires_in, int)
3239

33-
def test_init_from_nonexisting_file(self):
34-
with pytest.raises(IOError):
35-
credentials.Certificate(
36-
testutils.resource_filename('non_existing.json'))
37-
38-
def test_init_from_invalid_file(self):
39-
with pytest.raises(ValueError):
40-
credentials.Certificate(
41-
testutils.resource_filename('refresh_token.json'))
40+
@pytest.mark.parametrize('file_name,error', invalid_certs.values(), ids=list(invalid_certs))
41+
def test_init_from_invalid_certificate(self, file_name, error):
42+
with pytest.raises(error):
43+
credentials.Certificate(testutils.resource_filename(file_name))
4244

4345

4446
@pytest.fixture

tox.ini

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py27,py33
7+
envlist = py27,py33,pypy,cover
88

99
[testenv]
1010
commands = pytest
1111
deps =
1212
pytest
1313
oauth2client
1414
six
15+
16+
[coverbase]
17+
basepython = python2.7
18+
commands =
19+
pytest \
20+
--cov=firebase_admin \
21+
--cov=tests
22+
deps = {[testenv]deps}
23+
coverage
24+
pytest-cov
25+
26+
[testenv:cover]
27+
basepython = {[coverbase]basepython}
28+
commands =
29+
{[coverbase]commands}
30+
coverage report --show-missing
31+
deps =
32+
{[coverbase]deps}

0 commit comments

Comments
 (0)