[tests] Refactor setting up complex I/O mocking for modules

Modules now only have to have a single setup/teardown line in their code
to fully set up the I/O part of a test.
This commit is contained in:
Tobi-wan Kenobi 2017-03-05 13:01:28 +01:00
parent 69dceca7d0
commit 00849aa4fa
5 changed files with 35 additions and 77 deletions

View file

@ -3,15 +3,36 @@
import mock import mock
import json import json
import shlex import shlex
import random
import subprocess import subprocess
from bumblebee.input import I3BarInput
from bumblebee.output import Widget from bumblebee.output import Widget
from bumblebee.config import Config
import random
def rand(cnt): def rand(cnt):
return "".join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(cnt)) return "".join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(cnt))
def setup_test(test, Module):
test._stdin, test._select, test.stdin, test.select = epoll_mock("bumblebee.input")
test.popen = MockPopen()
test.config = Config()
test.input = I3BarInput()
test.engine = mock.Mock()
test.engine.input = test.input
test.input.need_event = True
test.module = Module(engine=test.engine, config={ "config": test.config })
for widget in test.module.widgets():
widget.link_module(test.module)
test.anyWidget = widget
def teardown_test(test):
test._stdin.stop()
test._select.stop()
test.popen.cleanup()
def epoll_mock(module=""): def epoll_mock(module=""):
if len(module) > 0: module = "{}.".format(module) if len(module) > 0: module = "{}.".format(module)

View file

@ -10,31 +10,15 @@ except ImportError:
import tests.mocks as mocks import tests.mocks as mocks
from bumblebee.config import Config from bumblebee.input import WHEEL_UP, WHEEL_DOWN
from bumblebee.input import I3BarInput, WHEEL_UP, WHEEL_DOWN
from bumblebee.modules.brightness import Module from bumblebee.modules.brightness import Module
class TestBrightnessModule(unittest.TestCase): class TestBrightnessModule(unittest.TestCase):
def setUp(self): def setUp(self):
self._stdin, self._select, self.stdin, self.select = mocks.epoll_mock("bumblebee.input") mocks.setup_test(self, Module)
self.popen = mocks.MockPopen()
self.config = Config()
self.input = I3BarInput()
self.engine = mock.Mock()
self.engine.input = self.input
self.input.need_event = True
self.module = Module(engine=self.engine, config={ "config": self.config })
for widget in self.module.widgets():
widget.link_module(self.module)
self.anyWidget = widget
def tearDown(self): def tearDown(self):
self._stdin.stop() mocks.teardown_test(self)
self._select.stop()
self.popen.cleanup()
def test_format(self): def test_format(self):
for widget in self.module.widgets(): for widget in self.module.widgets():

View file

@ -11,35 +11,19 @@ except ImportError:
import tests.mocks as mocks import tests.mocks as mocks
import bumblebee.input
from bumblebee.config import Config from bumblebee.config import Config
from bumblebee.input import I3BarInput, LEFT_MOUSE from bumblebee.input import LEFT_MOUSE
from bumblebee.modules.caffeine import Module from bumblebee.modules.caffeine import Module
class TestCaffeineModule(unittest.TestCase): class TestCaffeineModule(unittest.TestCase):
def setUp(self): def setUp(self):
self._stdin, self._select, self.stdin, self.select = mocks.epoll_mock("bumblebee.input") mocks.setup_test(self, Module)
self.popen = mocks.MockPopen()
self.input = I3BarInput()
self.engine = mock.Mock()
self.config = Config()
self.engine.input = self.input
self.engine.input.need_event = True
self.module = Module(engine=self.engine, config={ "config": self.config })
for widget in self.module.widgets():
widget.link_module(self.module)
self.anyWidget = widget
self.xset_active = " timeout: 0 cycle: 123" self.xset_active = " timeout: 0 cycle: 123"
self.xset_inactive = " timeout: 600 cycle: 123" self.xset_inactive = " timeout: 600 cycle: 123"
def tearDown(self): def tearDown(self):
self._stdin.stop() mocks.teardown_test(self)
self._select.stop()
self.popen.cleanup()
def test_text(self): def test_text(self):
self.assertEquals(self.module.caffeine(self.anyWidget), "") self.assertEquals(self.module.caffeine(self.anyWidget), "")

View file

@ -5,26 +5,12 @@ import unittest
import tests.mocks as mocks import tests.mocks as mocks
from bumblebee.config import Config from bumblebee.input import LEFT_MOUSE
from bumblebee.input import I3BarInput, LEFT_MOUSE
from bumblebee.modules.cmus import Module from bumblebee.modules.cmus import Module
class TestCmusModule(unittest.TestCase): class TestCmusModule(unittest.TestCase):
def setUp(self): def setUp(self):
self._stdin, self._select, self.stdin, self.select = mocks.epoll_mock("bumblebee.input") mocks.setup_test(self, Module)
self.popen = mocks.MockPopen()
self.config = Config()
self.input = I3BarInput()
self.engine = mock.Mock()
self.engine.input = self.input
self.input.need_event = True
self.module = Module(engine=self.engine, config={ "config": self.config })
for widget in self.module.widgets():
widget.link_module(self.module)
self.anyWidget = widget
self.songTemplate = """ self.songTemplate = """
status {status} status {status}
@ -40,9 +26,7 @@ tag comment comment
""" """
def tearDown(self): def tearDown(self):
self._stdin.stop() mocks.teardown_test(self)
self._select.stop()
self.popen.cleanup()
def test_read_song(self): def test_read_song(self):
self.popen.mock.communicate.return_value = ("song", None) self.popen.mock.communicate.return_value = ("song", None)

View file

@ -6,33 +6,18 @@ import mock
import tests.mocks as mocks import tests.mocks as mocks
from bumblebee.config import Config from bumblebee.input import LEFT_MOUSE
from bumblebee.input import I3BarInput, LEFT_MOUSE
from bumblebee.modules.cpu import Module from bumblebee.modules.cpu import Module
class TestCPUModule(unittest.TestCase): class TestCPUModule(unittest.TestCase):
def setUp(self): def setUp(self):
self._stdin, self._select, self.stdin, self.select = mocks.epoll_mock("bumblebee.input") mocks.setup_test(self, Module)
self.popen = mocks.MockPopen()
self._psutil = mock.patch("bumblebee.modules.cpu.psutil") self._psutil = mock.patch("bumblebee.modules.cpu.psutil")
self.psutil = self._psutil.start() self.psutil = self._psutil.start()
self.config = Config()
self.input = I3BarInput()
self.engine = mock.Mock()
self.engine.input = self.input
self.input.need_event = True
self.module = Module(engine=self.engine, config={ "config": self.config })
for widget in self.module.widgets():
widget.link_module(self.module)
self.anyWidget = widget
def tearDown(self): def tearDown(self):
self._stdin.stop()
self._select.stop()
self._psutil.stop() self._psutil.stop()
self.popen.cleanup() mocks.teardown_test(self)
def test_format(self): def test_format(self):
self.psutil.cpu_percent.return_value = 21.0 self.psutil.cpu_percent.return_value = 21.0