Skip to content

Commit 6df989e

Browse files
authored
Add more tests for tf (#539)
* More tests for tf * Fix flake8 errors * Beautify * Fix flake8 errors again
1 parent 985567b commit 6df989e

5 files changed

Lines changed: 61 additions & 14 deletions

File tree

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ matrix:
1212
- python: "3.7"
1313
env: TOX_ENV=py37
1414
- python: "3.6"
15-
env: TOX_ENV=tensorflow-1
15+
env: TOX_ENV=tensorflow-112
16+
- python: "3.6"
17+
env: TOX_ENV=tensorflow-113
18+
- python: "3.6"
19+
env: TOX_ENV=tensorflow-114
1620
- python: "3.6"
1721
env: TOX_ENV=tensorflow-2
1822
- python: "3.7"

sacred/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,12 @@ def apply_backspaces_and_linefeeds(text):
618618

619619
def module_exists(modname):
620620
"""Checks if a module exists without actually importing it."""
621-
return pkgutil.find_loader(modname) is not None
621+
try:
622+
return pkgutil.find_loader(modname) is not None
623+
except ImportError:
624+
# TODO: Temporary fix for tf 1.14.0.
625+
# Should be removed once fixed in tf.
626+
return True
622627

623628

624629
def modules_exist(*modnames):

tests/test_optional.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@
22
# coding=utf-8
33

44
import pytest
5-
from sacred.optional import optional_import
5+
from sacred.optional import optional_import, get_tensorflow, modules_exist
66

77

88
def test_optional_import():
9-
has_pytest, pyt = optional_import('pytest')
9+
has_pytest, pyt = optional_import("pytest")
1010
assert has_pytest
1111
assert pyt == pytest
1212

1313

1414
def test_optional_import_nonexisting():
15-
has_nonex, nonex = optional_import('clearlynonexistingpackage')
15+
has_nonex, nonex = optional_import("clearlynonexistingpackage")
1616
assert not has_nonex
1717
assert nonex is None
18+
19+
20+
def test_get_tensorflow():
21+
"""Test that get_tensorflow() runs without error."""
22+
pytest.importorskip("tensorflow")
23+
get_tensorflow()
24+
25+
26+
def test_module_exists_for_tensorflow():
27+
"""Check that module_exist returns true if tf is there."""
28+
pytest.importorskip("tensorflow")
29+
assert modules_exist("tensorflow")

tests/test_stflow/test_method_interception.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@pytest.fixture
1010
def ex():
11-
return Experiment('tensorflow_tests')
11+
return Experiment("tensorflow_tests")
1212

1313

1414
@pytest.fixture()
@@ -18,6 +18,7 @@ def tf():
1818
so `tensorflow` is not required during the tests.
1919
"""
2020
from sacred.optional import has_tensorflow
21+
2122
if has_tensorflow:
2223
return opt.get_tensorflow()
2324
else:
@@ -28,7 +29,10 @@ class FileWriter:
2829
def __init__(self, logdir, graph):
2930
self.logdir = logdir
3031
self.graph = graph
31-
print("Mocked FileWriter got logdir=%s, graph=%s" % (logdir, graph))
32+
print(
33+
"Mocked FileWriter got logdir=%s, graph=%s"
34+
% (logdir, graph)
35+
)
3236

3337
class Session:
3438
def __init__(self):
@@ -42,6 +46,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
4246

4347
# Set stflow to use the mock as the test
4448
import sacred.stflow.method_interception
49+
4550
sacred.stflow.method_interception.tf = tensorflow
4651
return tensorflow
4752

@@ -90,7 +95,10 @@ def run_experiment(_run):
9095
assert swr is not None
9196
assert _run.info["tensorflow"]["logdirs"] == [TEST_LOG_DIR]
9297
tf.summary.FileWriter(TEST_LOG_DIR2, s.graph)
93-
assert _run.info["tensorflow"]["logdirs"] == [TEST_LOG_DIR, TEST_LOG_DIR2]
98+
assert _run.info["tensorflow"]["logdirs"] == [
99+
TEST_LOG_DIR,
100+
TEST_LOG_DIR2,
101+
]
94102

95103
# This should not be captured:
96104
tf.summary.FileWriter("/tmp/whatever", s.graph)
@@ -133,7 +141,7 @@ def test_log_summary_writer_class(ex, tf):
133141
TEST_LOG_DIR = "/dev/null"
134142
TEST_LOG_DIR2 = "/tmp/sacred_test"
135143

136-
class FooClass():
144+
class FooClass:
137145
def __init__(self):
138146
pass
139147

tox.ini

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py35, py36, py37, setup, flake8, tensorflow-1, tensorflow-2
7+
envlist = py35, py36, py37, setup, flake8, tensorflow-112, tensorflow-113, tensorflow-114, tensorflow-2
88
# py32 does not work because of the 'wrapt' dependency
99
# py33 is not supported by newer versions of numpy
1010
# py34 is not supported by newer versions of pandas
@@ -16,20 +16,38 @@ commands =
1616
pytest \
1717
{posargs} # substitute with tox' positional arguments
1818

19-
[testenv:tensorflow-1]
19+
[testenv:tensorflow-112]
2020
basepython = python
2121
deps =
2222
-rdev-requirements.txt
23-
tensorflow==1.12.2
23+
tensorflow==1.12.3
2424
commands =
25-
pytest tests/test_stflow \
25+
pytest tests/test_stflow tests/test_optional.py \
26+
{posargs}
27+
28+
[testenv:tensorflow-113]
29+
basepython = python
30+
deps =
31+
-rdev-requirements.txt
32+
tensorflow==1.13.2
33+
commands =
34+
pytest tests/test_stflow tests/test_optional.py \
35+
{posargs}
36+
37+
[testenv:tensorflow-114]
38+
basepython = python
39+
deps =
40+
-rdev-requirements.txt
41+
tensorflow==1.14.0
42+
commands =
43+
pytest tests/test_stflow tests/test_optional.py \
2644
{posargs}
2745

2846
[testenv:tensorflow-2]
2947
basepython = python
3048
deps =
3149
-rdev-requirements.txt
32-
tensorflow==2.0.0-alpha0
50+
tensorflow==2.0.0-beta1
3351
commands =
3452
pytest tests/test_stflow \
3553
{posargs}

0 commit comments

Comments
 (0)