Convert test_qtutils to pytest

This commit is contained in:
Bruno Oliveira 2015-04-08 20:23:52 -03:00
parent 6037fd74cd
commit 853280feeb

View File

@ -20,14 +20,14 @@
"""Tests for qutebrowser.utils.qtutils.""" """Tests for qutebrowser.utils.qtutils."""
import sys import sys
import unittest
import pytest
from qutebrowser import qutebrowser from qutebrowser import qutebrowser
from qutebrowser.utils import qtutils from qutebrowser.utils import qtutils
class CheckOverflowTests(unittest.TestCase): class TestCheckOverflow:
"""Test check_overflow. """Test check_overflow.
Class attributes: Class attributes:
@ -58,69 +58,60 @@ class CheckOverflowTests(unittest.TestCase):
(float(INT64_MAX + 1), INT64_MAX)], (float(INT64_MAX + 1), INT64_MAX)],
} }
def test_good_values(self): @pytest.mark.parametrize('ctype, val', [(ctype, val) for ctype, vals in
GOOD_VALUES.items() for val in
vals])
def test_good_values(self, ctype, val):
"""Test values which are inside bounds.""" """Test values which are inside bounds."""
for ctype, vals in self.GOOD_VALUES.items():
for val in vals:
with self.subTest(ctype=ctype, val=val):
qtutils.check_overflow(val, ctype) qtutils.check_overflow(val, ctype)
def test_bad_values_fatal(self): @pytest.mark.parametrize('ctype, val',
[(ctype, val) for ctype, vals in BAD_VALUES.items()
for (val, _) in vals])
def test_bad_values_fatal(self, ctype, val):
"""Test values which are outside bounds with fatal=True.""" """Test values which are outside bounds with fatal=True."""
for ctype, vals in self.BAD_VALUES.items(): with pytest.raises(OverflowError):
for (val, _) in vals:
with self.subTest(ctype=ctype, val=val):
with self.assertRaises(OverflowError):
qtutils.check_overflow(val, ctype) qtutils.check_overflow(val, ctype)
def test_bad_values_nonfatal(self): @pytest.mark.parametrize('ctype, val, repl',
[(ctype, val, repl) for ctype, vals in
BAD_VALUES.items() for (val, repl) in vals])
def test_bad_values_nonfatal(self, ctype, val, repl):
"""Test values which are outside bounds with fatal=False.""" """Test values which are outside bounds with fatal=False."""
for ctype, vals in self.BAD_VALUES.items():
for (val, replacement) in vals:
with self.subTest(ctype=ctype, val=val):
newval = qtutils.check_overflow(val, ctype, fatal=False) newval = qtutils.check_overflow(val, ctype, fatal=False)
self.assertEqual(newval, replacement) assert newval == repl
def argparser_exit(status=0, message=None): # pylint: disable=unused-argument class TestGetQtArgs:
"""Function to monkey-patch .exit() of the argparser so it doesn't exit."""
raise Exception
class GetQtArgsTests(unittest.TestCase):
"""Tests for get_args.""" """Tests for get_args."""
def setUp(self): @pytest.fixture
self.parser = qutebrowser.get_argparser() def parser(self, mocker):
self.parser.exit = argparser_exit parser = qutebrowser.get_argparser()
# monkey-patch .exit() of the argparser so it doesn't exit.
mocker.patch.object(parser, 'exit', side_effect=Exception)
return parser
def test_no_qt_args(self): def test_no_qt_args(self, parser):
"""Test commandline with no Qt arguments given.""" """Test commandline with no Qt arguments given."""
args = self.parser.parse_args(['--debug']) args = parser.parse_args(['--debug'])
self.assertEqual(qtutils.get_args(args), [sys.argv[0]]) assert qtutils.get_args(args) == [sys.argv[0]]
def test_qt_flag(self): def test_qt_flag(self, parser):
"""Test commandline with a Qt flag.""" """Test commandline with a Qt flag."""
args = self.parser.parse_args(['--debug', '--qt-reverse', '--nocolor']) args = parser.parse_args(['--debug', '--qt-reverse', '--nocolor'])
self.assertEqual(qtutils.get_args(args), [sys.argv[0], '-reverse']) assert qtutils.get_args(args) == [sys.argv[0], '-reverse']
def test_qt_arg(self): def test_qt_arg(self, parser):
"""Test commandline with a Qt argument.""" """Test commandline with a Qt argument."""
args = self.parser.parse_args(['--qt-stylesheet', 'foobar']) args = parser.parse_args(['--qt-stylesheet', 'foobar'])
self.assertEqual(qtutils.get_args(args), [sys.argv[0], '-stylesheet', assert qtutils.get_args(args) == [sys.argv[0], '-stylesheet', 'foobar']
'foobar'])
def test_qt_both(self): def test_qt_both(self, parser):
"""Test commandline with a Qt argument and flag.""" """Test commandline with a Qt argument and flag."""
args = self.parser.parse_args(['--qt-stylesheet', 'foobar', args = parser.parse_args(['--qt-stylesheet', 'foobar', '--qt-reverse'])
'--qt-reverse'])
qt_args = qtutils.get_args(args) qt_args = qtutils.get_args(args)
self.assertEqual(qt_args[0], sys.argv[0]) assert qt_args[0] == sys.argv[0]
self.assertIn('-reverse', qt_args) assert '-reverse' in qt_args
self.assertIn('-stylesheet', qt_args) assert '-stylesheet' in qt_args
self.assertIn('foobar', qt_args) assert 'foobar' in qt_args
if __name__ == '__main__':
unittest.main()