Skip to content

Commit

Permalink
Merge pull request #174 from stevetracvc/feat/use-different-python
Browse files Browse the repository at this point in the history
PR: Use the Python interpreter set in Preferences to run tests
  • Loading branch information
ccordoba12 authored Jun 10, 2022
2 parents b788ad9 + 6642de0 commit 0b25301
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
uses: actions/checkout@v2
- name: Install System Packages
run: |
sudo apt-get update
sudo apt-get install libegl1-mesa
sudo apt-get update --fix-missing
sudo apt-get install -qq pyqt5-dev-tools libxcb-xinerama0 xterm --fix-missing
- name: Install Conda
uses: conda-incubator/setup-miniconda@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion requirements/conda.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
lxml
spyder>=5.2,<6
spyder>=5.3.1,<6
2 changes: 1 addition & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
codecov
flaky
nose
pytest
pytest>=5
pytest-cov
pytest-mock
pytest-qt
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_package_data(name, extlist):


# Requirements
REQUIREMENTS = ['lxml', 'spyder>=5.2,<6', 'pyzmq']
REQUIREMENTS = ['lxml', 'spyder>=5.3.1,<6', 'pyzmq']
EXTLIST = ['.jpg', '.png', '.json', '.mo', '.ini']
LIBNAME = 'spyder_unittest'

Expand Down
4 changes: 2 additions & 2 deletions spyder_unittest/backend/pytestrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def create_argument_list(self):
pyfile = os.path.join(os.path.dirname(__file__), 'pytestworker.py')
return [pyfile, str(self.reader.port)]

def start(self, config, pythonpath):
def start(self, config, executable, pythonpath):
"""Start process which will run the unit test suite."""
self.config = config
self.reader = ZmqStreamReader()
self.reader.sig_received.connect(self.process_output)
RunnerBase.start(self, config, pythonpath)
RunnerBase.start(self, config, executable, pythonpath)

def process_output(self, output):
"""
Expand Down
22 changes: 10 additions & 12 deletions spyder_unittest/backend/pytestworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
# Third party imports
import pytest

# Local imports
from spyder.config.base import get_translation
from spyder_unittest.backend.zmqstream import ZmqStreamWriter

# Local imports, needs to be relative otherwise it will fail if trying
# to execute in a different env with only spyder-kernel installed
try:
_ = get_translation('spyder_unittest')
except KeyError: # pragma: no cover
import gettext
_ = gettext.gettext

# this line is needed for the tests to succeed
from .zmqstream import ZmqStreamWriter
except:
# this line is needed for the plugin to work
from zmqstream import ZmqStreamWriter

class FileStub():
"""Stub for ZmqStreamWriter which instead writes to a file."""
Expand Down Expand Up @@ -104,8 +102,8 @@ def pytest_runtest_logreport(self, report):
self.was_skipped = True
if hasattr(report, 'wasxfail'):
self.was_xfail = True
self.longrepr.append(report.wasxfail if report.wasxfail else _(
'WAS EXPECTED TO FAIL'))
self.longrepr.append(report.wasxfail if report.wasxfail else
'WAS EXPECTED TO FAIL')
self.sections = report.sections # already accumulated over phases
if report.longrepr:
first_msg_idx = len(self.longrepr)
Expand All @@ -120,7 +118,7 @@ def pytest_runtest_logreport(self, report):
if report.outcome == 'failed' and report.when in (
'setup', 'teardown'):
self.longrepr[first_msg_idx] = '{} {}: {}'.format(
_('ERROR at'), report.when, self.longrepr[first_msg_idx])
'ERROR at', report.when, self.longrepr[first_msg_idx])

def pytest_runtest_logfinish(self, nodeid, location):
"""Called by pytest when the entire test is completed."""
Expand Down
6 changes: 3 additions & 3 deletions spyder_unittest/backend/runnerbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from qtpy.QtCore import (QObject, QProcess, QProcessEnvironment, QTextCodec,
Signal)
from spyder.py3compat import to_text_string
from spyder.utils.misc import get_python_executable

try:
from importlib.util import find_spec as find_spec_or_loader
Expand Down Expand Up @@ -189,7 +188,7 @@ def _prepare_process(self, config, pythonpath):
process.setProcessEnvironment(env)
return process

def start(self, config, pythonpath):
def start(self, config, executable, pythonpath):
"""
Start process which will run the unit test suite.
Expand All @@ -203,6 +202,8 @@ def start(self, config, pythonpath):
----------
config : TestConfig
Unit test configuration.
executable : str
Path to Python executable
pythonpath : list of str
List of directories to be added to the Python path
Expand All @@ -212,7 +213,6 @@ def start(self, config, pythonpath):
If process failed to start.
"""
self.process = self._prepare_process(config, pythonpath)
executable = get_python_executable()
p_args = self.create_argument_list()
try:
os.remove(self.resultfilename)
Expand Down
9 changes: 6 additions & 3 deletions spyder_unittest/backend/tests/test_pytestrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
# Standard library imports
import os
import os.path as osp
import sys

# Third party imports
import pytest
from qtpy.QtCore import QByteArray

# Local imports
from spyder_unittest.backend.pytestrunner import (PyTestRunner,
Expand Down Expand Up @@ -58,12 +58,13 @@ def test_pytestrunner_start(monkeypatch):

runner = PyTestRunner(None, 'results')
config = Config()
runner.start(config, ['pythondir'])
runner.start(config, sys.executable, ['pythondir'])
assert runner.config is config
assert runner.reader is mock_reader
runner.reader.sig_received.connect.assert_called_once_with(
runner.process_output)
MockRunnerBase.start.assert_called_once_with(runner, config, ['pythondir'])
MockRunnerBase.start.assert_called_once_with(
runner, config, sys.executable, ['pythondir'])


def test_pytestrunner_process_output_with_collected(qtbot):
Expand All @@ -75,6 +76,7 @@ def test_pytestrunner_process_output_with_collected(qtbot):
expected = ['spam.ham', 'eggs.bacon']
assert blocker.args == [expected]


def test_pytestrunner_process_output_with_collecterror(qtbot):
runner = PyTestRunner(None)
output = [{
Expand All @@ -87,6 +89,7 @@ def test_pytestrunner_process_output_with_collecterror(qtbot):
expected = [('ham.spam', 'msg')]
assert blocker.args == [expected]


def test_pytestrunner_process_output_with_starttest(qtbot):
runner = PyTestRunner(None)
output = [{'event': 'starttest', 'nodeid': 'ham/spam.py::ham'},
Expand Down
8 changes: 2 additions & 6 deletions spyder_unittest/backend/tests/test_runnerbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,13 @@ def test_runnerbase_start(monkeypatch):
monkeypatch.setattr('spyder_unittest.backend.runnerbase.os.remove',
mock_remove)

monkeypatch.setattr(
'spyder_unittest.backend.runnerbase.get_python_executable',
lambda: 'python')

runner = RunnerBase(None, 'results')
runner._prepare_process = lambda c, p: mock_process
runner.create_argument_list = lambda: ['arg1', 'arg2']
config = Config('pytest', 'wdir')
mock_process.waitForStarted = lambda: False
with pytest.raises(RuntimeError):
runner.start(config, ['pythondir'])
runner.start(config, 'python_exec', ['pythondir'])

mock_process.start.assert_called_once_with('python', ['arg1', 'arg2'])
mock_process.start.assert_called_once_with('python_exec', ['arg1', 'arg2'])
mock_remove.assert_called_once_with('results')
5 changes: 5 additions & 0 deletions spyder_unittest/widgets/tests/test_unittestgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Standard library imports
import os
import sys

# Third party imports
from qtpy.QtCore import Qt, QProcess
Expand All @@ -26,6 +27,10 @@
@pytest.fixture
def widget(qtbot):
unittest_widget = UnitTestWidget('testwidget', None, None)
unittest_widget.get_conf(
'executable',
section='main_interpreter',
default=sys.executable)
unittest_widget.setup()
qtbot.addWidget(unittest_widget)
return unittest_widget
Expand Down
4 changes: 2 additions & 2 deletions spyder_unittest/widgets/unittestgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ def run_tests(self, config=None):
self.testrunner.sig_starttest.connect(self.tests_started)
self.testrunner.sig_testresult.connect(self.tests_yield_result)
self.testrunner.sig_stop.connect(self.tests_stopped)

executable = self.get_conf('executable', section='main_interpreter')
try:
self.testrunner.start(config, pythonpath)
self.testrunner.start(config, executable, pythonpath)
except RuntimeError:
QMessageBox.critical(self,
_("Error"), _("Process failed to start"))
Expand Down

0 comments on commit 0b25301

Please sign in to comment.