From 1532040f0b47b456462368eb5fdc1a69c60ec03f Mon Sep 17 00:00:00 2001 From: enkore Date: Wed, 24 Jun 2015 11:01:41 +0200 Subject: [PATCH] Replace DIY parametrized tests with @pytest.mark.parametrize --- tests/test_battery.py | 35 +++++++++++---------- tests/test_core_util.py | 70 ++++++++++++++++------------------------- 2 files changed, 45 insertions(+), 60 deletions(-) diff --git a/tests/test_battery.py b/tests/test_battery.py index d4e7030..dd9c6b6 100644 --- a/tests/test_battery.py +++ b/tests/test_battery.py @@ -2,6 +2,8 @@ import os.path +import pytest + from i3pystatus import battery @@ -11,20 +13,19 @@ def battery_test(path, format, expected): assert bc.output["full_text"] == expected -def test_basic(): - cases = [ - ("test_battery_basic1", "FULL", "0.000", "0h:00m"), - ("test_battery_basic2", "FULL", "0.000", "0h:00m"), - ("test_battery_basic3", "DIS", "15.624", "4h:04m"), - ("test_battery_basic4", "DIS", "17.510", "1h:46m"), - ("test_battery_basic5", "DIS", "11.453", "4h:52m"), - ("test_battery_basic6", "CHR", "30.764", "0h:20m"), - ("test_battery_basic7", "DIS", "27.303", "1h:44m"), - ("test_battery_132", "DIS", "8.370", "1h:17m"), - ("test_battery_broken1", "FULL", "0.000", "0h:00m"), - ("test_battery_issue89_2", "DIS", "0.000", "0h:00m"), - ] - for path, status, consumption, remaining in cases: - battery_test(path, "{status}", status) - battery_test(path, "{consumption:.3f}", consumption) - battery_test(path, "{remaining:%hh:%Mm}", remaining) +@pytest.mark.parametrize("path, status, consumption, remaining", [ + ("test_battery_basic1", "FULL", "0.000", "0h:00m"), + ("test_battery_basic2", "FULL", "0.000", "0h:00m"), + ("test_battery_basic3", "DIS", "15.624", "4h:04m"), + ("test_battery_basic4", "DIS", "17.510", "1h:46m"), + ("test_battery_basic5", "DIS", "11.453", "4h:52m"), + ("test_battery_basic6", "CHR", "30.764", "0h:20m"), + ("test_battery_basic7", "DIS", "27.303", "1h:44m"), + ("test_battery_132", "DIS", "8.370", "1h:17m"), + ("test_battery_broken1", "FULL", "0.000", "0h:00m"), + ("test_battery_issue89_2", "DIS", "0.000", "0h:00m"), +]) +def test_basic(path, status, consumption, remaining): + battery_test(path, "{status}", status) + battery_test(path, "{consumption:.3f}", consumption) + battery_test(path, "{remaining:%hh:%Mm}", remaining) diff --git a/tests/test_core_util.py b/tests/test_core_util.py index 020326e..5b9680e 100644 --- a/tests/test_core_util.py +++ b/tests/test_core_util.py @@ -6,6 +6,8 @@ import string import random import types +import pytest + from i3pystatus.core.exceptions import ConfigAmbigiousClassesError, ConfigInvalidModuleError from i3pystatus.core import util, ClassFinder @@ -22,61 +24,43 @@ def test_lchop_unmatched(): assert util.lchop("12345", "345") == "12345" -def partition(iterable, limit, assrt): +@pytest.mark.parametrize("iterable, limit, assrt", [ + ([1, 2, 3, 4], 3, [[1, 2], [3], [4]]), + ([2, 1, 3, 4], 3, [[1, 2], [3], [4]]), + ([0.33, 0.45, 0.89], 1, [[0.33, 0.45, 0.89]]), + ([], 10, []), +]) +def test_partition(iterable, limit, assrt): partitions = util.partition(iterable, limit) partitions = [sorted(partition) for partition in partitions] for item in assrt: assert sorted(item) in partitions -def test_partition(): - cases = [ - ([1, 2, 3, 4], 3, [[1, 2], [3], [4]]), - ([2, 1, 3, 4], 3, [[1, 2], [3], [4]]), - ([0.33, 0.45, 0.89], 1, [[0.33, 0.45, 0.89]]), - ([], 10, []), - ] - - for iterable, limit, assrt in cases: - partition(iterable, limit, assrt) - - -def popwhile(iterable, predicate, assrt): +@pytest.mark.parametrize("iterable, predicate, assrt", [ + ([1, 2, 3, 4], lambda x: x < 2, []), + ([1, 2, 3, 4], lambda x: x < 5 and x > 2, [4, 3]), + ([1, 2, 3, 4], lambda x: x == 4, [4]), + ([1, 2, 3, 4], lambda x: True, [4, 3, 2, 1]), + ([1, 2], lambda x: False, []), +]) +def test_popwhile(iterable, predicate, assrt): assert list(util.popwhile(predicate, iterable)) == assrt -def test_popwhile(): - cases = [ - ([1, 2, 3, 4], lambda x: x < 2, []), - ([1, 2, 3, 4], lambda x: x < 5 and x > 2, [4, 3]), - ([1, 2, 3, 4], lambda x: x == 4, [4]), - ([1, 2, 3, 4], lambda x: True, [4, 3, 2, 1]), - ([1, 2], lambda x: False, []), - ] +@pytest.mark.parametrize("valid, required, feed, missing", [ + # ( valid, required, feed, missing ) + (("foo", "bar", "baz"), ("foo",), ("bar",), ("foo",)), + (("foo", "bar", "baz"), ("foo",), tuple(), ("foo",)), + (("foo", "bar", "baz"), ("bar", "baz"), ("bar", "baz"), tuple()), + (("foo", "bar", "baz"), ("bar", "baz"), ("bar", "foo", "baz"), tuple()), - for iterable, predicate, assrt in cases: - popwhile(iterable, predicate, assrt) - - -def keyconstraintdict_missing(valid, required, feedkeys, assrt_missing): +]) +def test_keyconstraintdict_missing(valid, required, feed, missing): kcd = util.KeyConstraintDict(valid_keys=valid, required_keys=required) - kcd.update(dict.fromkeys(feedkeys)) + kcd.update(dict.fromkeys(feed)) - assert kcd.missing() == set(assrt_missing) - - -def test_keyconstraintdict_missing(): - cases = [ - # ( valid, required, feed, missing ) - (("foo", "bar", "baz"), ("foo",), ("bar",), ("foo",)), - (("foo", "bar", "baz"), ("foo",), tuple(), ("foo",)), - (("foo", "bar", "baz"), ("bar", "baz"), ("bar", "baz"), tuple()), - (("foo", "bar", "baz"), ("bar", "baz"), - ("bar", "foo", "baz"), tuple()), - ] - - for valid, required, feed, missing in cases: - keyconstraintdict_missing(valid, required, feed, missing) + assert kcd.missing() == set(missing) class ModuleListTests(unittest.TestCase):