Created stubs fixture and converted test_stubs to pytest

This commit is contained in:
Bruno Oliveira 2015-04-03 18:12:49 -03:00
parent 9c533e1941
commit 3421e5e34f
2 changed files with 90 additions and 79 deletions

View File

@ -30,3 +30,12 @@ def app_and_logging(qapp):
""" """
from log import init from log import init
init() init()
@pytest.fixture(scope='session')
def stubs():
"""
Provides access to stub objects useful for testing.
"""
import stubs
return stubs

View File

@ -20,88 +20,90 @@
"""Test test stubs.""" """Test test stubs."""
import unittest
from unittest import mock from unittest import mock
from qutebrowser.test import stubs import pytest
class TestFakeTimer(unittest.TestCase): @pytest.fixture
def timer(stubs):
"""Test FakeTimer.""" return stubs.FakeTimer()
def setUp(self):
self.timer = stubs.FakeTimer() def test_timeout(timer):
"""Test whether timeout calls the functions."""
def test_timeout(self): func = mock.Mock()
"""Test whether timeout calls the functions.""" func2 = mock.Mock()
func = mock.Mock() timer.timeout.connect(func)
func2 = mock.Mock() timer.timeout.connect(func2)
self.timer.timeout.connect(func) assert not func.called
self.timer.timeout.connect(func2) assert not func2.called
self.assertFalse(func.called) timer.timeout.emit()
self.assertFalse(func2.called) func.assert_called_once_with()
self.timer.timeout.emit() func2.assert_called_once_with()
func.assert_called_once_with()
func2.assert_called_once_with()
def test_disconnect_all(timer):
def test_disconnect_all(self): """Test disconnect without arguments."""
"""Test disconnect without arguments.""" func = mock.Mock()
func = mock.Mock() timer.timeout.connect(func)
self.timer.timeout.connect(func) timer.timeout.disconnect()
self.timer.timeout.disconnect() timer.timeout.emit()
self.timer.timeout.emit() assert not func.called
self.assertFalse(func.called)
def test_disconnect_one(self): def test_disconnect_one(timer):
"""Test disconnect with a single argument.""" """Test disconnect with a single argument."""
func = mock.Mock() func = mock.Mock()
self.timer.timeout.connect(func) timer.timeout.connect(func)
self.timer.timeout.disconnect(func) timer.timeout.disconnect(func)
self.timer.timeout.emit() timer.timeout.emit()
self.assertFalse(func.called) assert not func.called
def test_disconnect_all_invalid(self):
"""Test disconnecting with no connections.""" def test_disconnect_all_invalid(timer):
with self.assertRaises(TypeError): """Test disconnecting with no connections."""
self.timer.timeout.disconnect() with pytest.raises(TypeError):
timer.timeout.disconnect()
def test_disconnect_one_invalid(self):
"""Test disconnecting with an invalid connection."""
func1 = mock.Mock() def test_disconnect_one_invalid(timer):
func2 = mock.Mock() """Test disconnecting with an invalid connection."""
self.timer.timeout.connect(func1) func1 = mock.Mock()
with self.assertRaises(TypeError): func2 = mock.Mock()
self.timer.timeout.disconnect(func2) timer.timeout.connect(func1)
self.assertFalse(func1.called) with pytest.raises(TypeError):
self.assertFalse(func2.called) timer.timeout.disconnect(func2)
self.timer.timeout.emit() assert not func1.called
func1.assert_called_once_with() assert not func2.called
timer.timeout.emit()
def test_singleshot(self): func1.assert_called_once_with()
"""Test setting singleShot."""
self.assertFalse(self.timer.singleShot())
self.timer.setSingleShot(True) def test_singleshot(timer):
self.assertTrue(self.timer.singleShot()) """Test setting singleShot."""
self.timer.start() assert not timer.singleShot()
self.assertTrue(self.timer.isActive()) timer.setSingleShot(True)
self.timer.timeout.emit() assert timer.singleShot()
self.assertFalse(self.timer.isActive()) timer.start()
assert timer.isActive()
def test_active(self): timer.timeout.emit()
"""Test isActive.""" assert not timer.isActive()
self.assertFalse(self.timer.isActive())
self.timer.start()
self.assertTrue(self.timer.isActive()) def test_active(timer):
self.timer.stop() """Test isActive."""
self.assertFalse(self.timer.isActive()) assert not timer.isActive()
timer.start()
def test_interval(self): assert timer.isActive()
"""Test setting an interval.""" timer.stop()
self.assertEqual(self.timer.interval(), 0) assert not timer.isActive()
self.timer.setInterval(1000)
self.assertEqual(self.timer.interval(), 1000)
def test_interval(timer):
"""Test setting an interval."""
assert timer.interval() == 0
timer.setInterval(1000)
assert timer.interval() == 1000
if __name__ == '__main__':
unittest.main()