Skip to content

Commit

Permalink
Add gui test
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Feb 17, 2020
1 parent d35ea76 commit f6cea47
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 13 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ addons:
before_install:
- if [[ $PYTHON_VERSION == 2.7 ]]; then
export TRAVIS_PYTHON_VERSION="2.7";
export PYTEST_QT_API="pyqt4v2";
else
export TRAVIS_PYTHON_VERSION="3.6";
fi
Expand Down Expand Up @@ -91,15 +92,18 @@ install:
- bash .build_install.sh libres

script:
- ulimit -n 2048
- export PYTHONPATH=$INSTALL_DIR/lib/python$TRAVIS_PYTHON_VERSION/dist-packages:$PYTHONPATH
- export PYTHONPATH=$INSTALL_DIR/lib/python$TRAVIS_PYTHON_VERSION/site-packages:$PYTHONPATH
- export PATH=$INSTALL_DIR/bin:$PATH
- echo $PATH
- echo $PYTHONPATH
- pip install -r dev-requirements.txt
- pip install . --prefix=$INSTALL_DIR
- python setup.py test
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
xvfb-run python setup.py test;
else
python setup.py test;
fi
- sphinx-build -n -v -E -W ./docs/rst/manual ./tmp/ert_docs
- conda remove pyqt -y
- ert --help
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pytest
pytest-qt
mock
sphinx
sphinx-argparse
Expand Down
22 changes: 14 additions & 8 deletions ert_gui/gert_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def run_gui(args):
app = QApplication([]) # Early so that QT is initialized before other imports
app.setWindowIcon(resourceIcon("application/window_icon_cutout"))

res_config = ResConfig(args.config)
os.chdir(res_config.config_path)
ert = EnKFMain(res_config, strict=True, verbose=args.verbose)

_start_window(ert, args.config)

return app.exec_()


def _start_window(ert, config):

_check_locale()

help_center = HelpCenter("ERT")
Expand All @@ -57,12 +68,9 @@ def run_gui(args):
splash.repaint()
splash_screen_start_time = time.time()

res_config = ResConfig(args.config)
os.chdir(res_config.config_path)
ert = EnKFMain(res_config, strict=True, verbose=args.verbose)
configureErtNotifier(ert, args.config)
configureErtNotifier(ert, config)

window = _setup_main_window(args.config, ert)
window = _setup_main_window(config, ert)

minimum_splash_screen_time = 2
sleep_time_left = minimum_splash_screen_time - (time.time() - splash_screen_start_time)
Expand All @@ -87,9 +95,7 @@ def run_gui(args):
"No observations loaded. Model update algorithms disabled!",
)

finished_code = app.exec_()
return finished_code

return window

def _check_locale():
# There seems to be a setlocale() call deep down in the initialization of
Expand Down
1 change: 1 addition & 0 deletions ert_gui/simulation/ensemble_experiment_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class EnsembleExperimentPanel(SimulationConfigPanel):

def __init__(self):
SimulationConfigPanel.__init__(self, EnsembleExperiment)
self.setObjectName("Ensemble_experiment_panel")

layout = QFormLayout()

Expand Down
3 changes: 2 additions & 1 deletion ert_gui/simulation/simulation_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ class SimulationPanel(QWidget):
def __init__(self, config_file):
QWidget.__init__(self)
self._config_file = config_file

self.setObjectName("Simulation_panel")
layout = QVBoxLayout()

self._simulation_mode_combo = QComboBox()
self._simulation_mode_combo.setObjectName("Simulation_mode")
addHelpToWidget(self._simulation_mode_combo, "run/simulation_mode")

self._simulation_mode_combo.currentIndexChanged.connect(self.toggleSimulationMode)
Expand Down
3 changes: 1 addition & 2 deletions ert_gui/simulation/single_test_run_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
class SingleTestRunPanel(SimulationConfigPanel):

def __init__(self):

SimulationConfigPanel.__init__(self, SingleTestRun)

self.setObjectName("Single_test_run_panel")
layout = QFormLayout()

case_selector = CaseSelector()
Expand Down
137 changes: 137 additions & 0 deletions tests/gui/test_gui_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import sys
import os
import pytest

import ert_gui
from ert_gui.tools import HelpCenter

from distutils.version import StrictVersion
from qtpy.QtCore import Qt
import qtpy
from ert_gui.gert_main import _start_window, run_gui

if sys.version_info >= (3, 3):
from unittest.mock import Mock, PropertyMock
else:
from mock import Mock, PropertyMock


@pytest.fixture()
def patch_enkf_main(monkeypatch, tmpdir):
plugins_mock = Mock()
plugins_mock.getPluginJobs.return_value = []

mocked_enkf_main = Mock()
mocked_enkf_main.getWorkflowList.return_value = plugins_mock

res_config_mock = Mock()
type(res_config_mock).config_path = PropertyMock(return_value=tmpdir.strpath)

monkeypatch.setattr(
ert_gui.gert_main, "EnKFMain", Mock(return_value=mocked_enkf_main)
)
monkeypatch.setattr(
ert_gui.gert_main, "ResConfig", Mock(return_value=res_config_mock)
)
monkeypatch.setattr(
ert_gui.ertwidgets.caseselector.CaseSelector,
"_getAllCases",
Mock(return_value=["test"]),
)
monkeypatch.setattr(
ert_gui.ertwidgets.caseselector, "getCurrentCaseName", Mock(return_value="test")
)
monkeypatch.setattr(
ert_gui.ertwidgets.models.activerealizationsmodel,
"getRealizationCount",
Mock(return_value=0),
)
monkeypatch.setattr(
ert_gui.simulation.ensemble_experiment_panel,
"getRealizationCount",
Mock(return_value=0),
)
monkeypatch.setattr(
ert_gui.ertwidgets.models.activerealizationsmodel,
"mask_to_rangestring",
Mock(return_value=""),
)

obs_mock = Mock()
obs_mock.have_observations.return_value = False
ert_shared_mock = Mock()
type(ert_shared_mock).ert = PropertyMock(return_value=obs_mock)

monkeypatch.setattr(ert_gui.simulation.simulation_panel, "ERT", ert_shared_mock)
monkeypatch.setattr(
ert_gui.ertwidgets.summarypanel.ErtSummary,
"getForwardModels",
Mock(return_value=[]),
)
monkeypatch.setattr(
ert_gui.ertwidgets.summarypanel.ErtSummary,
"getParameters",
Mock(return_value=[]),
)
monkeypatch.setattr(
ert_gui.ertwidgets.summarypanel.ErtSummary,
"getObservations",
Mock(return_value=[]),
)
monkeypatch.setattr(
ert_gui.tools.export.export_keyword_model.ExportKeywordModel,
"hasKeywords",
Mock(return_value=False),
)
monkeypatch.setattr(
ert_gui.tools.workflows.workflows_tool,
"getWorkflowNames",
Mock(return_value=[]),
)

yield mocked_enkf_main
# Clean up:
HelpCenter._HelpCenter__help_centers = {}


@pytest.mark.skipif(
StrictVersion(qtpy.PYQT_VERSION) < StrictVersion("5.0")
and os.environ.get("PYTEST_QT_API") != "pyqt4v2",
reason="PyQt4 with PYTEST_QT_API env. variable != pyqt4v2",
)
@pytest.mark.skipif(
os.environ.get("TRAVIS_OS_NAME") == "osx", reason="xvfb not available on travis OSX"
)
def test_gui_load(monkeypatch, tmpdir, qtbot, patch_enkf_main):

gui = _start_window(patch_enkf_main, "config.ert")
qtbot.addWidget(gui)

sim_panel = gui.findChild(qtpy.QtWidgets.QWidget, name="Simulation_panel")
single_run_panel = gui.findChild(qtpy.QtWidgets.QWidget, name="Single_test_run_panel")
assert sim_panel.getCurrentSimulationModel() == single_run_panel.getSimulationModel()

sim_mode = gui.findChild(qtpy.QtWidgets.QWidget, name="Simulation_mode")
qtbot.keyClick(sim_mode, Qt.Key_Down)

ensamble_panel = gui.findChild(qtpy.QtWidgets.QWidget, name="Ensemble_experiment_panel")
assert sim_panel.getCurrentSimulationModel() == ensamble_panel.getSimulationModel()


@pytest.mark.skipif(
os.environ.get("TRAVIS_OS_NAME") == "osx", reason="xvfb not available on travis OSX"
)
@pytest.mark.skipif(
StrictVersion(qtpy.PYQT_VERSION) < StrictVersion("5.0")
and os.environ.get("PYTEST_QT_API") != "pyqt4v2",
reason="PyQt4 with PYTEST_QT_API env. variable != pyqt4v2",
)
@pytest.mark.usefixtures("patch_enkf_main")
def test_gui_full(monkeypatch, tmpdir, qapp):

args_mock = Mock()
type(args_mock).config = PropertyMock(return_value="config.ert")

qapp.exec_ = lambda: None # exec_ starts the event loop, and will stall the test.
monkeypatch.setattr(ert_gui.gert_main, "QApplication", Mock(return_value=qapp))
gui = run_gui(args_mock)

0 comments on commit f6cea47

Please sign in to comment.