From 51128934d96bdda824a67b6d35ffed997e887839 Mon Sep 17 00:00:00 2001 From: Jon Bernard Date: Wed, 30 Dec 2015 06:42:10 -0500 Subject: Use pytest parametrize to simplify tests --- tests/test_dotfile.py | 106 ++++++++++++++++---------------------------------- 1 file changed, 33 insertions(+), 73 deletions(-) (limited to 'tests') diff --git a/tests/test_dotfile.py b/tests/test_dotfile.py index b854859..e679caf 100644 --- a/tests/test_dotfile.py +++ b/tests/test_dotfile.py @@ -3,34 +3,21 @@ import py.error from dotfiles.dotfile import Dotfile -class TestAdd: +@pytest.mark.parametrize("times", range(1, 4)) +def test_add(tmpdir, times): - def test_add(self, tmpdir): + repo = tmpdir.ensure("Dotfiles", dir=1) + name = tmpdir.ensure(".vimrc") + target = repo.join("vimrc") - repo = tmpdir.ensure("Dotfiles", dir=1) - name = tmpdir.ensure(".vimrc") - target = repo.join("vimrc") + dotfile = Dotfile(name, target) + dotfile.add() - dotfile = Dotfile(name, target) - dotfile.add() - - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) - - def test_add_twice(self, tmpdir): - - repo = tmpdir.ensure("Dotfiles", dir=1) - name = tmpdir.ensure(".vimrc") - target = repo.join("vimrc") - - dotfile = Dotfile(name, target) - dotfile.add() - - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + assert target.check(file=1, link=0) + assert name.check(file=1, link=1) + assert name.samefile(target) + for x in range(2, times): with pytest.raises(OSError): dotfile.add() @@ -39,36 +26,22 @@ class TestAdd: assert name.samefile(target) -class TestRemove: - - def test_remove(self, tmpdir): - - repo = tmpdir.ensure("Dotfiles", dir=1) - name = tmpdir.join(".vimrc") - target = repo.ensure("vimrc") - - name.mksymlinkto(target) - - dotfile = Dotfile(name, target) - dotfile.remove() - - assert False == target.check() - assert name.check(file=1, link=0) +@pytest.mark.parametrize("times", range(1, 4)) +def test_remove(tmpdir, times): - def test_remove_twice(self, tmpdir): + repo = tmpdir.ensure("Dotfiles", dir=1) + name = tmpdir.join(".vimrc") + target = repo.ensure("vimrc") - repo = tmpdir.ensure("Dotfiles", dir=1) - name = tmpdir.join(".vimrc") - target = repo.ensure("vimrc") + name.mksymlinkto(target) - name.mksymlinkto(target) + dotfile = Dotfile(name, target) + dotfile.remove() - dotfile = Dotfile(name, target) - dotfile.remove() - - assert False == target.check() - assert name.check(file=1, link=0) + assert False == target.check() + assert name.check(file=1, link=0) + for x in range(2, times): with pytest.raises(OSError): dotfile.remove() @@ -76,34 +49,21 @@ class TestRemove: assert name.check(file=1, link=0) -class TestSync: - - def test_sync(self, tmpdir): - - repo = tmpdir.ensure("Dotfiles", dir=1) - name = tmpdir.join(".vimrc") - target = repo.ensure("vimrc") - - dotfile = Dotfile(name, target) - dotfile.sync() +@pytest.mark.parametrize("times", range(1, 4)) +def test_sync(tmpdir, times): - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) - - def test_sync_twice(self, tmpdir): + repo = tmpdir.ensure("Dotfiles", dir=1) + name = tmpdir.join(".vimrc") + target = repo.ensure("vimrc") - repo = tmpdir.ensure("Dotfiles", dir=1) - name = tmpdir.join(".vimrc") - target = repo.ensure("vimrc") + dotfile = Dotfile(name, target) + dotfile.sync() - dotfile = Dotfile(name, target) - dotfile.sync() - - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + assert target.check(file=1, link=0) + assert name.check(file=1, link=1) + assert name.samefile(target) + for x in range(2, times): with pytest.raises(py.error.EEXIST): dotfile.sync() -- cgit v1.2.3