Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Fix issues with inconsistent tests #1018

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions pydm/tests/utilities/test_stylesheet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import pytest
import logging


from ... import config
from ...utilities import stylesheet
from qtpy.QtWidgets import QApplication

Expand All @@ -11,14 +12,26 @@
"..", "test_data", "global_stylesheet.css")


def test_stylesheet_apply(qtbot):
# Backup of the variable
@pytest.fixture(scope='function')
def save_and_restore_pydm_stylesheet():
"""
A fixture for ensuring that modifications to the PYDM_STYLESHEET environment variable are restored
even if the test fails. This will prevent any impact to the user's environment, as well as future tests in the run.
"""
# Back up the stylesheet related variables so they can be restored after the test
env_backup = os.getenv("PYDM_STYLESHEET", None)
backup_global_style_path = stylesheet.GLOBAL_STYLESHEET
os.environ["PYDM_STYLESHEET"] = ""

# Backup of the GLOBAL_STYLESHEET path
backup_global = stylesheet.GLOBAL_STYLESHEET
yield

os.environ["PYDM_STYLESHEET"] = ""
# Restore the user's original stylesheet
if env_backup:
os.environ["PYDM_STYLESHEET"] = env_backup
stylesheet.GLOBAL_STYLESHEET = backup_global_style_path


def test_stylesheet_apply(qtbot, save_and_restore_pydm_stylesheet):
assert os.getenv("PYDM_STYLESHEET", None) == ""

# Retrieve instance of the application so we can test with it
Expand All @@ -33,9 +46,6 @@ def test_stylesheet_apply(qtbot):

assert app.styleSheet() is not None

# Backup of the GLOBAL_STYLESHEET path
backup_global = stylesheet.GLOBAL_STYLESHEET

stylesheet.clear_cache()
# Exercise when there is no stylesheet available
stylesheet.GLOBAL_STYLESHEET = "invalid_file.none"
Expand All @@ -47,20 +57,8 @@ def test_stylesheet_apply(qtbot):

assert not app.styleSheet()

# Restore the variable
if env_backup:
os.environ["PYDM_STYLESHEET"] = env_backup
stylesheet.GLOBAL_STYLESHEET = backup_global


def test_stylesheet_get_style_data(caplog):
# Backup of the variable
env_backup = os.getenv("PYDM_STYLESHEET", None)

# Backup of the GLOBAL_STYLESHEET path
backup_global = stylesheet.GLOBAL_STYLESHEET

os.environ["PYDM_STYLESHEET"] = ""
def test_stylesheet_get_style_data(caplog, save_and_restore_pydm_stylesheet):
assert os.getenv("PYDM_STYLESHEET", None) == ""

with caplog.at_level(logging.DEBUG):
Expand Down Expand Up @@ -127,10 +125,8 @@ def test_stylesheet_get_style_data(caplog):
ret = stylesheet._get_style_data()

# Make sure logging capture the error, and have the correct error message
assert len(caplog.records) == 1
if not config.STYLESHEET:
assert len(caplog.records) == 1
else:
assert len(caplog.records) == 2 # Extra message about existing stylesheet
assert "Cannot find the default stylesheet" in caplog.text

# Restore the variable
if env_backup:
os.environ["PYDM_STYLESHEET"] = env_backup
stylesheet.GLOBAL_STYLESHEET = backup_global
13 changes: 8 additions & 5 deletions pydm/tests/widgets/test_lineedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ def wait_focus():
else:
pydm_lineedit.clearFocus()
def wait_nofocus():
pydm_lineedit.clearFocus()
return not pydm_lineedit.hasFocus()

qtbot.waitUntil(wait_nofocus, timeout=5000)
Expand Down Expand Up @@ -589,29 +590,31 @@ def test_focus_in_event(qtbot, qapp, displayed_value, focus_reason, expected_foc
with qtbot.waitExposed(pydm_lineedit):
pydm_lineedit.show()

def wait_focus(focus_state):
def wait_focus(focus_state, wait_on_clear):
""" Verify the current focus state of the line edit """
if focus_state:
pydm_lineedit.setFocus(Qt.OtherFocusReason)
elif wait_on_clear:
pydm_lineedit.clearFocus()
qapp.processEvents()
return pydm_lineedit.hasFocus() == focus_state

# First set the current focus status opposite of that which we expect, to ensure we are actually
# measuring the correct end state
if expected_focus:
pydm_lineedit.clearFocus()
qtbot.waitUntil(functools.partial(wait_focus, False), timeout=5000)
qtbot.waitUntil(functools.partial(wait_focus, False, True), timeout=5000)
else:
pydm_lineedit.setFocus(Qt.OtherFocusReason)
qtbot.waitUntil(functools.partial(wait_focus, True), timeout=5000)
qtbot.waitUntil(functools.partial(wait_focus, True, False), timeout=5000)

# Set the focus, verify the result is what we are expecting
if expected_focus:
pydm_lineedit.setFocus(focus_reason)
else:
event_to_send = QFocusEvent(QEvent.FocusIn, reason=focus_reason)
pydm_lineedit.focusInEvent(event_to_send)
qtbot.waitUntil(functools.partial(wait_focus, expected_focus), timeout=5000)
qtbot.waitUntil(functools.partial(wait_focus, expected_focus, False), timeout=5000)


@pytest.mark.parametrize("display_value", [
Expand Down Expand Up @@ -655,9 +658,9 @@ def wait_focus():
qtbot.waitUntil(wait_focus, timeout=5000)

pydm_lineedit.setText("Canceled after the focusOut event")
pydm_lineedit.clearFocus()

def wait_nofocus():
pydm_lineedit.clearFocus()
qapp.processEvents()
return not pydm_lineedit.hasFocus()

Expand Down
Loading