Skip to content

Commit

Permalink
Testing: Fix test.sh on macOS and add fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Jan 27, 2022
1 parent 49c0dbd commit aa01584
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ eval "$(conda shell.bash hook)"
conda remove -q -n test-env --all || true

# Create and activate conda environment for this test
QT_VERSION_VAR=${1^^}_QT_VERSION
BINDING=$(echo "$1" | tr '[:lower:]' '[:upper:]')
QT_VERSION_VAR=${BINDING}_QT_VERSION
if [ "${!QT_VERSION_VAR:0:3}" = "5.9" ]; then PYTESTQT_VERSION="=3.3.0"; fi # pytest-qt >=4 doesn't support Qt >=5.9
conda create -q -n test-env python=${PYTHON_VERSION} pytest 'pytest-cov>=3.0.0' pytest-qt${PYTESTQT_VERSION:-}
conda activate test-env
Expand Down
12 changes: 12 additions & 0 deletions qtpy/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

import pytest


def pytest_configure(config):
"""Configure the test environment."""
Expand Down Expand Up @@ -64,3 +66,13 @@ def pytest_report_header(config):
versions += os.linesep

return versions


@pytest.fixture
def pdf_writer(qtbot):
from pathlib import Path
from qtpy import QtGui
output_path = Path('test.pdf')
device = QtGui.QPdfWriter(str(output_path))
yield device, output_path
output_path.unlink()
7 changes: 3 additions & 4 deletions qtpy/tests/test_qtgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ def test_qguiapplication_functions():

@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
reason="Segmentation fault/Aborted on Linux CI when not using conda")
def test_qtextdocument_functions():
def test_qtextdocument_functions(pdf_writer):
"""Test functions mapping for QtGui.QTextDocument."""
assert QtGui.QTextDocument.print_ is not None
text_document = QtGui.QTextDocument("Test")
print_device = QtGui.QPdfWriter('test.pdf')
print_device, output_path = pdf_writer
text_document.print_(print_device)
assert os.path.exists('test.pdf')
os.remove('test.pdf')
assert os.path.exists(output_path)


@pytest.mark.skipif(PYQT5 and PYQT_VERSION.startswith('5.9'),
Expand Down
20 changes: 13 additions & 7 deletions qtpy/tests/test_qtsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from qtpy import PYSIDE2, PYSIDE_VERSION, QtSql


@pytest.fixture
def database_connection():
"""Create a database connection"""
connection = QtSql.QSqlDatabase.addDatabase("QSQLITE")
yield connection
connection.close()


def test_qtsql():
"""Test the qtpy.QtSql namespace"""
assert QtSql.QSqlDatabase is not None
Expand All @@ -29,7 +37,7 @@ def test_qtsql():
@pytest.mark.skipif(
sys.platform == 'win32' and PYSIDE2 and PYSIDE_VERSION.startswith('5.13'),
reason="SQLite driver unavailable on PySide 5.13.2 with Windows")
def test_qtsql_members_aliases():
def test_qtsql_members_aliases(database_connection):
"""
Test aliased methods over qtpy.QtSql members including:
Expand All @@ -41,11 +49,10 @@ def test_qtsql_members_aliases():
assert QtSql.QSqlQuery.exec_ is not None
assert QtSql.QSqlResult.exec_ is not None

connection = QtSql.QSqlDatabase.addDatabase("QSQLITE")
assert connection.open()
connection.setDatabaseName("test.sqlite")
assert database_connection.open()
database_connection.setDatabaseName("test.sqlite")
QtSql.QSqlDatabase.exec_(
connection,
database_connection,
"""
CREATE TABLE test (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
Expand All @@ -54,7 +61,7 @@ def test_qtsql_members_aliases():
"""
)
# Created table 'test' and 'sqlite_sequence'
assert len(connection.tables()) == 2
assert len(database_connection.tables()) == 2

insert_table_query = QtSql.QSqlQuery()
assert insert_table_query.exec_(
Expand All @@ -72,4 +79,3 @@ def test_qtsql_members_aliases():
select_table_query.exec_()
record = select_table_query.record()
assert not record.isEmpty()
connection.close()
15 changes: 7 additions & 8 deletions qtpy/tests/test_qtwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,32 @@

@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
reason="Fatal Python error: Aborted on Linux CI when not using conda")
def test_qtextedit_functions(qtbot):
def test_qtextedit_functions(qtbot, pdf_writer):
"""Test functions mapping for QtWidgets.QTextEdit."""
assert QtWidgets.QTextEdit.setTabStopWidth
assert QtWidgets.QTextEdit.tabStopWidth
assert QtWidgets.QTextEdit.print_
textedit_widget = QtWidgets.QTextEdit(None)
textedit_widget.setTabStopWidth(90)
assert textedit_widget.tabStopWidth() == 90
print_device = QtGui.QPdfWriter('test.pdf')
print_device, output_path = pdf_writer
textedit_widget.print_(print_device)
assert os.path.exists('test.pdf')
os.remove('test.pdf')
assert os.path.exists(output_path)


@pytest.mark.skipif(sys.platform.startswith('linux') and os.environ.get('USE_CONDA', 'No') == 'No',
reason="Fatal Python error: Aborted on Linux CI when not using conda")
def test_qplaintextedit_functions(qtbot):
def test_qplaintextedit_functions(qtbot, pdf_writer):
"""Test functions mapping for QtWidgets.QPlainTextEdit."""
assert QtWidgets.QPlainTextEdit.setTabStopWidth
assert QtWidgets.QPlainTextEdit.tabStopWidth
assert QtWidgets.QPlainTextEdit.print_
plaintextedit_widget = QtWidgets.QPlainTextEdit(None)
plaintextedit_widget.setTabStopWidth(90)
assert plaintextedit_widget.tabStopWidth() == 90
print_device = QtGui.QPdfWriter('test.pdf')
print_device, output_path = pdf_writer
plaintextedit_widget.print_(print_device)
assert os.path.exists('test.pdf')
os.remove('test.pdf')
assert os.path.exists(output_path)


def test_qapplication_functions():
Expand Down Expand Up @@ -87,6 +85,7 @@ def test_qmenu_functions(qtbot):
QtCore.QTimer.singleShot(100, menu.close)
menu.exec_()


@pytest.mark.skipif(PYQT5 and PYQT_VERSION.startswith('5.9'),
reason="A specific setup with at least sip 4.9.9 is needed for PyQt5 5.9.*"
"to work with scoped enum access")
Expand Down

0 comments on commit aa01584

Please sign in to comment.