diff --git a/clickplc/tests/test_driver.py b/clickplc/tests/test_driver.py index e23a666..6387eb3 100644 --- a/clickplc/tests/test_driver.py +++ b/clickplc/tests/test_driver.py @@ -1,4 +1,5 @@ """Test the driver correctly parses a tags file and responds with correct data.""" +import asyncio from unittest import mock import pytest @@ -6,14 +7,24 @@ from clickplc import command_line from clickplc.mock import ClickPLC +ADDRESS = 'fakeip' +# from clickplc.driver import ClickPLC +# ADDRESS = '172.16.0.168' -@pytest.fixture -def plc_driver(): - """Confirm the driver correctly initializes without a tags file.""" - return ClickPLC('fake ip') +@pytest.fixture(scope="session") +def event_loop(): + """Override the default event_loop fixture (which is function-scoped) to be session-scoped.""" + loop = asyncio.new_event_loop() + yield loop + loop.close() +@pytest.fixture(scope='session') +async def plc_driver(): + """Confirm the driver correctly initializes without a tags file.""" + async with ClickPLC(ADDRESS) as c: + yield c @pytest.fixture def expected_tags(): @@ -43,7 +54,7 @@ def expected_tags(): @mock.patch('clickplc.ClickPLC', ClickPLC) def test_driver_cli(capsys): """Confirm the commandline interface works without a tags file.""" - command_line(['fakeip']) + command_line([ADDRESS]) captured = capsys.readouterr() assert 'x816' in captured.out assert 'c100' in captured.out @@ -53,26 +64,26 @@ def test_driver_cli(capsys): @mock.patch('clickplc.ClickPLC', ClickPLC) def test_driver_cli_tags(capsys): """Confirm the commandline interface works with a tags file.""" - command_line(['fakeip', 'clickplc/tests/plc_tags.csv']) + command_line([ADDRESS, 'clickplc/tests/plc_tags.csv']) captured = capsys.readouterr() assert 'P_101' in captured.out assert 'VAHH_101_OK' in captured.out assert 'TI_101' in captured.out with pytest.raises(SystemExit): - command_line(['fakeip', 'tags', 'bogus']) + command_line([ADDRESS, 'tags', 'bogus']) - -def test_unsupported_tags(): +@pytest.mark.asyncio +async def test_unsupported_tags(): """Confirm the driver detects an improper tags file.""" with pytest.raises(TypeError, match='unsupported data type'): - ClickPLC('fake ip', 'clickplc/tests/bad_tags.csv') + ClickPLC(ADDRESS, 'clickplc/tests/bad_tags.csv') @pytest.mark.asyncio async def test_tagged_driver(expected_tags): """Test a roundtrip with the driver using a tags file.""" - async with ClickPLC('fakeip', 'clickplc/tests/plc_tags.csv') as tagged_driver: + async with ClickPLC(ADDRESS, 'clickplc/tests/plc_tags.csv') as tagged_driver: await tagged_driver.set('VAH_101_OK', True) state = await tagged_driver.get() assert state.get('VAH_101_OK') diff --git a/setup.cfg b/setup.cfg index 660341c..e75b86c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,3 +5,4 @@ ignore_missing_imports = True [tool:pytest] addopts = --cov=clickplc +asyncio_mode = auto