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):
return stubs.FakeTimer()
"""Test FakeTimer."""
def setUp(self): def test_timeout(timer):
self.timer = stubs.FakeTimer()
def test_timeout(self):
"""Test whether timeout calls the functions.""" """Test whether timeout calls the functions."""
func = mock.Mock() func = mock.Mock()
func2 = mock.Mock() func2 = mock.Mock()
self.timer.timeout.connect(func) timer.timeout.connect(func)
self.timer.timeout.connect(func2) timer.timeout.connect(func2)
self.assertFalse(func.called) assert not func.called
self.assertFalse(func2.called) assert not func2.called
self.timer.timeout.emit() timer.timeout.emit()
func.assert_called_once_with() func.assert_called_once_with()
func2.assert_called_once_with() func2.assert_called_once_with()
def test_disconnect_all(self):
def test_disconnect_all(timer):
"""Test disconnect without arguments.""" """Test disconnect without arguments."""
func = mock.Mock() func = mock.Mock()
self.timer.timeout.connect(func) timer.timeout.connect(func)
self.timer.timeout.disconnect() timer.timeout.disconnect()
self.timer.timeout.emit() timer.timeout.emit()
self.assertFalse(func.called) assert not 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):
def test_disconnect_all_invalid(timer):
"""Test disconnecting with no connections.""" """Test disconnecting with no connections."""
with self.assertRaises(TypeError): with pytest.raises(TypeError):
self.timer.timeout.disconnect() timer.timeout.disconnect()
def test_disconnect_one_invalid(self):
def test_disconnect_one_invalid(timer):
"""Test disconnecting with an invalid connection.""" """Test disconnecting with an invalid connection."""
func1 = mock.Mock() func1 = mock.Mock()
func2 = mock.Mock() func2 = mock.Mock()
self.timer.timeout.connect(func1) timer.timeout.connect(func1)
with self.assertRaises(TypeError): with pytest.raises(TypeError):
self.timer.timeout.disconnect(func2) timer.timeout.disconnect(func2)
self.assertFalse(func1.called) assert not func1.called
self.assertFalse(func2.called) assert not func2.called
self.timer.timeout.emit() timer.timeout.emit()
func1.assert_called_once_with() func1.assert_called_once_with()
def test_singleshot(self):
def test_singleshot(timer):
"""Test setting singleShot.""" """Test setting singleShot."""
self.assertFalse(self.timer.singleShot()) assert not timer.singleShot()
self.timer.setSingleShot(True) timer.setSingleShot(True)
self.assertTrue(self.timer.singleShot()) assert timer.singleShot()
self.timer.start() timer.start()
self.assertTrue(self.timer.isActive()) assert timer.isActive()
self.timer.timeout.emit() timer.timeout.emit()
self.assertFalse(self.timer.isActive()) assert not timer.isActive()
def test_active(self):
def test_active(timer):
"""Test isActive.""" """Test isActive."""
self.assertFalse(self.timer.isActive()) assert not timer.isActive()
self.timer.start() timer.start()
self.assertTrue(self.timer.isActive()) assert timer.isActive()
self.timer.stop() timer.stop()
self.assertFalse(self.timer.isActive()) assert not timer.isActive()
def test_interval(self):
def test_interval(timer):
"""Test setting an interval.""" """Test setting an interval."""
self.assertEqual(self.timer.interval(), 0) assert timer.interval() == 0
self.timer.setInterval(1000) timer.setInterval(1000)
self.assertEqual(self.timer.interval(), 1000) assert timer.interval() == 1000
if __name__ == '__main__':
unittest.main()