Skip to content

Commit

Permalink
refactor view tests with parametrized fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
xflr6 committed May 10, 2017
1 parent f8513d6 commit 4030a5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 56 deletions.
23 changes: 3 additions & 20 deletions tests/conftest.py
Expand Up @@ -11,26 +11,9 @@ def svg_pattern():
return re.compile(r'(?s)^<\?xml .+</svg>\s*$')


@pytest.fixture
def unknown_platform(name='spam'):
with mock.patch('graphviz.backend.PLATFORM', name):
yield name


@pytest.fixture
def darwin(name='darwin'):
with mock.patch('graphviz.backend.PLATFORM', name):
yield name


@pytest.fixture
def unixoid(name='linux'):
with mock.patch('graphviz.backend.PLATFORM', name):
yield name


@pytest.fixture
def windows(name='windows'):
@pytest.fixture(params=['', 'darwin', 'freebsd', 'linux', 'windows'])
def platform(request):
name = request.param
with mock.patch('graphviz.backend.PLATFORM', name):
yield name

Expand Down
32 changes: 14 additions & 18 deletions tests/test_backend.py
Expand Up @@ -36,22 +36,18 @@ def test_pipe(svg_pattern):
assert svg_pattern.match(src)


def test_view_unknown(unknown_platform):
with pytest.raises(RuntimeError) as e:
def test_view(platform, Popen, startfile):
if not platform:
with pytest.raises(RuntimeError) as e:
view('spam')
e.match(r'platform')
else:
view('spam')
e.match(r'platform')


def test_view_darwin(darwin, Popen):
view('spam')
Popen.assert_called_once_with(['open', 'spam'])


def test_view_unixoid(unixoid, Popen):
view('spam')
Popen.assert_called_once_with(['xdg-open', 'spam'])


def test_view_windows(windows, startfile):
view('spam')
startfile.assert_called_once_with('spam')
if platform == 'darwin':
Popen.assert_called_once_with(['open', 'spam'])
elif platform in ('linux', 'freebsd'):
Popen.assert_called_once_with(['xdg-open', 'spam'])
elif platform == 'windows':
startfile.assert_called_once_with('spam')
else:
raise RuntimeError
32 changes: 14 additions & 18 deletions tests/test_files.py
Expand Up @@ -63,25 +63,21 @@ def test_render_noent(file_noent):
e.match(r'failed to execute')


def test_view_unknown(unknown_platform, file):
with pytest.raises(RuntimeError) as e:
def test_view_unknown(platform, Popen, startfile, file):
if not platform:
with pytest.raises(RuntimeError) as e:
file._view('name', 'png')
e.match(r'support')
else:
file._view('name', 'png')
e.match(r'support')


def test_view_darwin(darwin, Popen, file):
file._view('name', 'png')
Popen.assert_called_once_with(['open', 'name'])


def test_view_unixoid(unixoid, Popen, file):
file._view('name', 'png')
Popen.assert_called_once_with(['xdg-open', 'name'])


def test_view_windows(windows, startfile, file):
file._view('name', 'png')
startfile.assert_called_once_with('name')
if platform == 'darwin':
Popen.assert_called_once_with(['open', 'name'])
elif platform in ('freebsd', 'linux'):
Popen.assert_called_once_with(['xdg-open', 'name'])
elif platform == 'windows':
startfile.assert_called_once_with('name')
else:
raise RuntimeError


def test_source():
Expand Down

0 comments on commit 4030a5a

Please sign in to comment.