Skip to content

Commit b491bbc

Browse files
authored
Use pytest_configure instead of import directly. (#754)
1 parent 9d32b0d commit b491bbc

File tree

6 files changed

+31
-33
lines changed

6 files changed

+31
-33
lines changed

test/conftest.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import pytest
12
import platform
23
from pkg_resources import parse_version
34

45

5-
IS_DARWIN = platform.system().lower() == "darwin"
6-
IS_WINDOWS = platform.system().lower() == "windows"
6+
def pytest_configure():
7+
pytest.IS_DARWIN = platform.system().lower() == "darwin"
8+
pytest.IS_WINDOWS = platform.system().lower() == "windows"
79

8-
if IS_DARWIN:
9-
# check for SIERRA
10-
if parse_version("10.12") < parse_version(platform.mac_ver()[0]):
11-
SERIAL_PORT = '/dev/ptyp0'
10+
if pytest.IS_DARWIN:
11+
# check for SIERRA
12+
if parse_version("10.12") < parse_version(platform.mac_ver()[0]):
13+
pytest.SERIAL_PORT = '/dev/ptyp0'
14+
else:
15+
pytest.SERIAL_PORT = '/dev/ttyp0'
1216
else:
13-
SERIAL_PORT = '/dev/ttyp0'
14-
else:
15-
SERIAL_PORT = "/dev/ptmx"
17+
pytest.SERIAL_PORT = "/dev/ptmx"

test/test_client_async.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from pymodbus.transaction import ModbusSocketFramer, ModbusTlsFramer, ModbusRtuFramer
2626
from pymodbus.transaction import ModbusAsciiFramer, ModbusBinaryFramer
2727
from pymodbus.client.asynchronous.twisted import ModbusSerClientProtocol
28-
from test.conftest import SERIAL_PORT
2928

3029
import ssl
3130

@@ -191,7 +190,7 @@ def testSerialTwistedClient(self, method, framer):
191190

192191
protocol, client = AsyncModbusSerialClient(schedulers.REACTOR,
193192
method=method,
194-
port=SERIAL_PORT,
193+
port=pytest.SERIAL_PORT,
195194
proto_cls=ModbusSerClientProtocol)
196195

197196
assert (isinstance(client, SerialPort))
@@ -219,12 +218,12 @@ def testSerialTornadoClient(self, method, framer):
219218
""" Test the serial tornado client client initialize """
220219
from serial import Serial
221220
with maybe_manage(sys.platform in ('darwin', 'win32'), patch.object(Serial, "open")):
222-
protocol, future = AsyncModbusSerialClient(schedulers.IO_LOOP, method=method, port=SERIAL_PORT)
221+
protocol, future = AsyncModbusSerialClient(schedulers.IO_LOOP, method=method, port=pytest.SERIAL_PORT)
223222
client = future.result()
224223
assert(isinstance(client, AsyncTornadoModbusSerialClient))
225224
assert(0 == len(list(client.transaction)))
226225
assert(isinstance(client.framer, framer))
227-
assert(client.port == SERIAL_PORT)
226+
assert(client.port == pytest.SERIAL_PORT)
228227
assert(client._connected)
229228

230229
def handle_failure(failure):
@@ -251,11 +250,11 @@ def testSerialAsyncioClient(self, mock_gather, mock_event_loop, method, framer):
251250
"""
252251
loop = asyncio.get_event_loop()
253252
loop.is_running.side_effect = lambda: False
254-
loop, client = AsyncModbusSerialClient(schedulers.ASYNC_IO, method=method, port=SERIAL_PORT, loop=loop,
253+
loop, client = AsyncModbusSerialClient(schedulers.ASYNC_IO, method=method, port=pytest.SERIAL_PORT, loop=loop,
255254
baudrate=19200, parity='E', stopbits=2, bytesize=7)
256255
assert(isinstance(client, AsyncioModbusSerialClient))
257256
assert(isinstance(client.framer, framer))
258-
assert(client.port == SERIAL_PORT)
257+
assert(client.port == pytest.SERIAL_PORT)
259258
assert(client.baudrate == 19200)
260259
assert(client.parity == 'E')
261260
assert(client.stopbits == 2)

test/test_client_async_tornado.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
import unittest
33
from unittest.mock import patch, Mock
4+
import pytest
45

56
from pymodbus.client.asynchronous.tornado import (BaseTornadoClient,
67
AsyncModbusSerialClient, AsyncModbusUDPClient, AsyncModbusTCPClient
@@ -11,7 +12,6 @@
1112
from pymodbus.exceptions import ConnectionException
1213
from pymodbus.transaction import ModbusSocketFramer, ModbusRtuFramer
1314
from pymodbus.bit_read_message import ReadCoilsRequest, ReadCoilsResponse
14-
from test.conftest import SERIAL_PORT
1515

1616
# ---------------------------------------------------------------------------#
1717
# Fixture
@@ -166,7 +166,7 @@ def testSerialClientInit(self):
166166
client = AsyncModbusSerialClient(ioloop=schedulers.IO_LOOP,
167167
framer=ModbusRtuFramer(
168168
ClientDecoder()),
169-
port=SERIAL_PORT)
169+
port=pytest.SERIAL_PORT)
170170
self.assertEqual(0, len(list(client.transaction)))
171171
self.assertTrue(isinstance(client.framer, ModbusRtuFramer))
172172

@@ -182,8 +182,8 @@ def testSerialClientConnect(self, mock_serial, mock_seriostream, mock_ioloop):
182182
client = AsyncModbusSerialClient(ioloop=schedulers.IO_LOOP,
183183
framer=ModbusRtuFramer(
184184
ClientDecoder()),
185-
port=SERIAL_PORT)
186-
self.assertTrue(client.port, SERIAL_PORT)
185+
port=pytest.SERIAL_PORT)
186+
self.assertTrue(client.port, pytest.SERIAL_PORT)
187187
self.assertFalse(client._connected)
188188
client.connect()
189189
self.assertTrue(client._connected)
@@ -197,7 +197,7 @@ def testSerialClientDisconnect(self, mock_serial, mock_seriostream, mock_ioloop)
197197
client = AsyncModbusSerialClient(ioloop=schedulers.IO_LOOP,
198198
framer=ModbusRtuFramer(
199199
ClientDecoder()),
200-
port=SERIAL_PORT)
200+
port=pytest.SERIAL_PORT)
201201
client.connect()
202202
self.assertTrue(client._connected)
203203

@@ -217,7 +217,7 @@ def testSerialClientExecute(self, mock_serial, mock_seriostream, mock_ioloop):
217217
client = AsyncModbusSerialClient(ioloop=schedulers.IO_LOOP,
218218
framer=ModbusRtuFramer(
219219
ClientDecoder()),
220-
port=SERIAL_PORT,
220+
port=pytest.SERIAL_PORT,
221221
timeout=0)
222222
client.connect()
223223
client.stream = Mock()
@@ -237,7 +237,7 @@ def testSerialClientHandleResponse(self, mock_serial, mock_seriostream, mock_iol
237237
client = AsyncModbusSerialClient(ioloop=schedulers.IO_LOOP,
238238
framer=ModbusRtuFramer(
239239
ClientDecoder()),
240-
port=SERIAL_PORT)
240+
port=pytest.SERIAL_PORT)
241241
client.connect()
242242
out = []
243243
reply = ReadCoilsRequest(1, 1)
@@ -261,7 +261,7 @@ def testSerialClientBuildResponse(self, mock_serial, mock_seriostream, mock_iolo
261261
client = AsyncModbusSerialClient(ioloop=schedulers.IO_LOOP,
262262
framer=ModbusRtuFramer(
263263
ClientDecoder()),
264-
port=SERIAL_PORT)
264+
port=pytest.SERIAL_PORT)
265265
self.assertEqual(0, len(list(client.transaction)))
266266

267267
def handle_failure(failure):

test/test_server_async.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pymodbus.compat import byte2int
1515
from pymodbus.transaction import ModbusSocketFramer
1616
from pymodbus.exceptions import NoSuchSlaveException, ModbusIOException
17-
from test.conftest import SERIAL_PORT
1817

1918
import sys
2019
# --------------------------------------------------------------------------- #
@@ -183,7 +182,7 @@ def testUdpServerStartup(self):
183182
def testSerialServerStartup(self, mock_sp):
184183
''' Test that the modbus serial asynchronous server starts correctly '''
185184
with patch('twisted.internet.reactor') as mock_reactor:
186-
StartSerialServer(context=None, port=SERIAL_PORT)
185+
StartSerialServer(context=None, port=pytest.SERIAL_PORT)
187186
self.assertEqual(mock_reactor.run.call_count, 1)
188187

189188
@no_twisted_serial_on_windows_with_pypy
@@ -194,7 +193,7 @@ def testStopServerFromMainThread(self, mock_sp):
194193
:return:
195194
"""
196195
with patch('twisted.internet.reactor') as mock_reactor:
197-
StartSerialServer(context=None, port=SERIAL_PORT)
196+
StartSerialServer(context=None, port=pytest.SERIAL_PORT)
198197
self.assertEqual(mock_reactor.run.call_count, 1)
199198
StopServer()
200199
self.assertEqual(mock_reactor.stop.call_count, 1)
@@ -209,7 +208,7 @@ def testStopServerFromThread(self, mock_sp):
209208
from threading import Thread
210209
import time
211210
with patch('twisted.internet.reactor') as mock_reactor:
212-
StartSerialServer(context=None, port=SERIAL_PORT)
211+
StartSerialServer(context=None, port=pytest.SERIAL_PORT)
213212
self.assertEqual(mock_reactor.run.call_count, 1)
214213
t = Thread(target=StopServer)
215214
t.start()

test/test_server_asyncio.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818

1919
from pymodbus.exceptions import NoSuchSlaveException
20-
from test.conftest import IS_WINDOWS, SERIAL_PORT
2120

2221
# ---------------------------------------------------------------------------#
2322
# Fixture
@@ -112,7 +111,7 @@ def eof_received(self):
112111
self.assertTrue( process.call_args[1]["data"] == data )
113112
server.server_close()
114113

115-
@pytest.mark.skipif(IS_WINDOWS, reason="To fix")
114+
@pytest.mark.skipif(pytest.IS_WINDOWS, reason="To fix")
116115
async def testTcpServerRoundtrip(self):
117116
''' Test sending and receiving data on tcp socket '''
118117
data = b"\x01\x00\x00\x00\x00\x06\x01\x03\x00\x00\x00\x01" # unit 1, read register
@@ -150,7 +149,7 @@ def eof_received(self):
150149
await asyncio.sleep(0)
151150
server.server_close()
152151

153-
@pytest.mark.skipif(IS_WINDOWS, reason="To fix")
152+
@pytest.mark.skipif(pytest.IS_WINDOWS, reason="To fix")
154153
async def testTcpServerConnectionLost(self):
155154
""" Test tcp stream interruption """
156155

test/test_server_sync.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
import unittest
33
from unittest.mock import patch, Mock
4-
4+
import pytest
55
import serial
66
import socket
77
import ssl
@@ -20,7 +20,6 @@
2020
from pymodbus.transaction import ModbusTlsFramer
2121

2222
from pymodbus.compat import socketserver
23-
from test.conftest import SERIAL_PORT
2423

2524

2625
# --------------------------------------------------------------------------- #
@@ -400,7 +399,7 @@ def testStartUdpServer(self):
400399
def testStartSerialServer(self):
401400
''' Test the serial server starting factory '''
402401
with patch.object(ModbusSerialServer, 'serve_forever') as mock_server:
403-
StartSerialServer(port=SERIAL_PORT)
402+
StartSerialServer(port=pytest.SERIAL_PORT)
404403

405404

406405
# --------------------------------------------------------------------------- #

0 commit comments

Comments
 (0)