Skip to content

Commit

Permalink
Merge pull request #540 from pyvisa/test-reorganization
Browse files Browse the repository at this point in the history
Test reorganization
  • Loading branch information
MatthieuDartiailh committed Aug 26, 2020
2 parents 8688eff + 3ab014d commit 293cc1d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 78 deletions.
5 changes: 3 additions & 2 deletions docs/source/introduction/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ contain the following::

Please note that `[Paths]` is treated case-sensitively.

To specify extra .dll search paths on Windows, use a `.pyvisarc` file like the following::
To specify extra .dll search paths on Windows, use a `.pyvisarc` file like the
following::

[Paths]

Expand All @@ -80,7 +81,7 @@ You can define a site-wide configuration file at
:file:`/usr/share/pyvisa/.pyvisarc` (It may also be
:file:`/usr/local/...` depending on the location of your Python).
Under Windows, this file is usually placed at
:file:`c:\\Python27\\share\\pyvisa\\.pyvisarc`.
:file:`c:\\Python37\\share\\pyvisa\\.pyvisarc`.

If you encounter any problem, take a look at the :ref:`faq`. There you will
find the solutions to common problem as well as useful debugging techniques. If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from pyvisa.constants import EventType, ResourceAttribute
from pyvisa.resources import Resource

from .resource_utils import ResourceTestCase
from .resource_utils import (
EventAwareResourceTestCaseMixin,
LockableResourceTestCaseMixin,
ResourceTestCase,
)

try:
import numpy as np # type: ignore
Expand Down Expand Up @@ -682,6 +686,12 @@ def test_stb(self):
assert 0 <= self.instr.stb <= 256
assert 0 <= self.instr.read_stb() <= 256


class EventAwareMessagebasedResourceTestCaseMixin(EventAwareResourceTestCaseMixin):
"""Mixin for message based resources supporting events.
"""

def test_manually_called_handlers(self):
"""Test calling manually even handler."""

Expand Down Expand Up @@ -783,70 +793,6 @@ def test_getting_unknown_buffer(self):
"""
assert self.instr.visalib.get_buffer_from_id(1) is None

def test_shared_locking(self):
"""Test locking/unlocking a resource.
"""
instr2 = self.rm.open_resource(str(self.rname))
instr3 = self.rm.open_resource(str(self.rname))

key = self.instr.lock()
instr2.lock(requested_key=key)

assert self.instr.query("*IDN?")
assert instr2.query("*IDN?")
with pytest.raises(errors.VisaIOError):
instr3.query("*IDN?")

# Share the lock for a limited time
with instr3.lock_context(requested_key=key) as key2:
assert instr3.query("*IDN?")
assert key == key2

# Stop sharing the lock
instr2.unlock()

with pytest.raises(errors.VisaIOError):
instr2.query("*IDN?")
with pytest.raises(errors.VisaIOError):
instr3.query("*IDN?")

self.instr.unlock()

assert instr3.query("*IDN?")

def test_exclusive_locking(self):
"""Test locking/unlocking a resource.
"""
instr2 = self.rm.open_resource(str(self.rname))

self.instr.lock_excl()
with pytest.raises(errors.VisaIOError):
instr2.query("*IDN?")

self.instr.unlock()

assert instr2.query("*IDN?")

# Share the lock for a limited time
with self.instr.lock_context(requested_key="exclusive") as key:
assert key is None
with pytest.raises(errors.VisaIOError):
instr2.query("*IDN?")

# Skipped since I do not know how to test those without service request.
test_wait_on_event = pytest.mark.skip(ResourceTestCase.test_wait_on_event)
test_managing_visa_handler = pytest.mark.skip(
ResourceTestCase.test_managing_visa_handler
)


class SRQMessagebasedResourceTestCase(MessagebasedResourceTestCase):
"""Base tests for message based resources supporting Service Request and queue.
"""

def test_wait_on_event_timeout(self):
"""Test waiting on a VISA event.
Expand Down Expand Up @@ -1008,3 +954,59 @@ def test_bare_handler(self):
assert self.instr.read() == "1"
finally:
ctwrapper.WRAP_HANDLER = True


class LockableMessagedBasedResourceTestCaseMixin(LockableResourceTestCaseMixin):
"""Mixing for message based resources supporting locking."""

def test_shared_locking(self):
"""Test locking/unlocking a resource.
"""
instr2 = self.rm.open_resource(str(self.rname))
instr3 = self.rm.open_resource(str(self.rname))

key = self.instr.lock()
instr2.lock(requested_key=key)

assert self.instr.query("*IDN?")
assert instr2.query("*IDN?")
with pytest.raises(errors.VisaIOError):
instr3.query("*IDN?")

# Share the lock for a limited time
with instr3.lock_context(requested_key=key) as key2:
assert instr3.query("*IDN?")
assert key == key2

# Stop sharing the lock
instr2.unlock()

with pytest.raises(errors.VisaIOError):
instr2.query("*IDN?")
with pytest.raises(errors.VisaIOError):
instr3.query("*IDN?")

self.instr.unlock()

assert instr3.query("*IDN?")

def test_exclusive_locking(self):
"""Test locking/unlocking a resource.
"""
instr2 = self.rm.open_resource(str(self.rname))

self.instr.lock_excl()
with pytest.raises(errors.VisaIOError):
instr2.query("*IDN?")

self.instr.unlock()

assert instr2.query("*IDN?")

# Share the lock for a limited time
with self.instr.lock_context(requested_key="exclusive") as key:
assert key is None
with pytest.raises(errors.VisaIOError):
instr2.query("*IDN?")
8 changes: 8 additions & 0 deletions pyvisa/testsuite/keysight_assisted_tests/resource_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def test_attribute_handling(self):
)
assert self.instr.timeout == float("+inf")


class EventAwareResourceTestCaseMixin:
"""Mixing for resources supporting handling events."""

def test_wait_on_event(self):
"""Test waiting on a VISA event.
Expand All @@ -203,6 +207,10 @@ def test_managing_visa_handler(self):
"""
raise NotImplementedError()


class LockableResourceTestCaseMixin:
"""Mixing for resources supporting locking."""

def test_shared_locking(self):
"""Test locking/unlocking a resource.
Expand Down
18 changes: 7 additions & 11 deletions pyvisa/testsuite/keysight_assisted_tests/test_tcpip_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@

from . import copy_func, require_virtual_instr
from .messagebased_resource_utils import (
EventAwareMessagebasedResourceTestCaseMixin,
LockableMessagedBasedResourceTestCaseMixin,
MessagebasedResourceTestCase,
SRQMessagebasedResourceTestCase,
)


@require_virtual_instr
class TestTCPIPInstr(SRQMessagebasedResourceTestCase):
class TestTCPIPInstr(
LockableMessagedBasedResourceTestCaseMixin,
EventAwareMessagebasedResourceTestCaseMixin,
MessagebasedResourceTestCase,
):
"""Test pyvisa against a TCPIP INSTR resource.
"""
Expand Down Expand Up @@ -116,12 +121,3 @@ class TestTCPIPSocket(MessagebasedResourceTestCase):
test_no_delay_args_in_query_binary = pytest.mark.xfail(
copy_func(MessagebasedResourceTestCase.test_no_delay_args_in_query_binary)
)
test_manual_async_read = pytest.mark.xfail(
copy_func(MessagebasedResourceTestCase.test_manual_async_read)
)
test_shared_locking = pytest.mark.xfail(
copy_func(MessagebasedResourceTestCase.test_shared_locking)
)
test_exclusive_locking = pytest.mark.xfail(
copy_func(MessagebasedResourceTestCase.test_exclusive_locking)
)

0 comments on commit 293cc1d

Please sign in to comment.