Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/matrix-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ jobs:
run: |
pip install -e .[all]
- name: Run tests
run: python run_tests.py all
run: pytest
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
pip install -e .[all]

- name: Run tests on Windows
run: python run_tests.py all
run: pytest

- name: Make sure the CLI outputs utf8 on Windows
# Note: we're checking something CLI specific, from a prompt, so we don't want to run
Expand Down Expand Up @@ -167,4 +167,4 @@ jobs:
run: pip install httpx pytest

- name: unit test the web API
run: python -m tests.test_web_api
run: pytest tests/test_web_api.py
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ or test the installed version (requires installing dev dependencies):

pip install 'readalongs[dev]' # if you installed from PyPI, or
pip install -e '.[dev]' # if you installed from a local clone
python run_tests.py

recently, we also made the project compatible with Pytest, so this will run the tests too:

pytest

And you can download our [open samples on GitHub](https://github.com/ReadAlongs/OpenSamples)
Expand Down
67 changes: 12 additions & 55 deletions tests/basic_test_case.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"""Common base class for the ReadAlongs test suites"""
"""Common base class for the ReadAlongs test suites"""

import logging
import os
import sys
import tempfile
from contextlib import contextmanager
from pathlib import Path
from unittest import TestCase

import pytest
from click.testing import CliRunner

import readalongs.text.make_package as make_package
Expand All @@ -16,8 +13,8 @@
make_package.FETCH_BUNDLE_TIMEOUT_SECONDS = 5 # shorter timeout for testing


class BasicTestCase(TestCase):
"""A Basic Unittest build block class that comes bundled with
class BasicTestCase:
"""A pytest building block class that comes bundled with
a temporary directory (self.tempdir), the path to the test data (self.data_dir)

For convenience, self.tempdir and self.data_dir are pathlib.Path objects
Expand All @@ -37,13 +34,9 @@ class BasicTestCase(TestCase):
# self.keep_temp_dir_after_running = True
keep_temp_dir_after_running = False

def setUp(self):
"""Create a temporary directory, self.tempdir, and a test runner, self.runner

If a subclass needs its own setUp() function, make sure to call
super().setUp()
at the beginning of it.
"""
@pytest.fixture(autouse=True)
def _pytest_setup(self):
"""Create per-test temporary state and run an optional subclass hook."""
self.runner = CliRunner()
tempdir_prefix = f"tmpdir_{type(self).__name__}_"
if not self.keep_temp_dir_after_running:
Expand All @@ -57,13 +50,12 @@ def setUp(self):
print(f"tmpdir={tempdir_name}")
self.tempdir = Path(tempdir_name)

def tearDown(self):
"""Clean up the temporary directory
setup = getattr(self, "_setUp", None)
if setup is not None:
setup()

yield

If a subclass needs its own tearDown() function, make sure to call
super().tearDown()
at the end of it.
"""
if not self.keep_temp_dir_after_running:
self.tempdirobj.cleanup()

Expand All @@ -72,38 +64,3 @@ def tearDown(self):
# Some test cases can set the logging level to DEBUG when they pass
# --debug to a CLI command, but don't let that affect subsequent tests.
LOGGER.setLevel(logging.INFO)


@contextmanager
def silence_c_stderr():
"""Capture stderr from C output, e.g., from SoundSwallower.

Note: to capture stderr for both C and Python code, combine this with
redirect_stderr(), but you must use capture_c_stderr() first:
with capture_c_stderr(), redirect_stderr(io.StringIO()):
# code

Loosely inspired by https://stackoverflow.com/a/24277852, but much simplified to
address our narrow needs, namely to silence stderr in a context manager.
"""

if "pytest" in sys.modules or os.name == "nt" and sys.version_info < (3, 10):
# Incompatible and pointless with pytest since it captures all output
# Also work around instability for this on Windows with Py 3.8/3.9
yield
else:
stderr_fileno = sys.stderr.fileno()
stderr_save = os.dup(stderr_fileno)
stderr_fd = os.open(os.devnull, os.O_RDWR)
os.dup2(stderr_fd, stderr_fileno)
yield
os.dup2(stderr_save, stderr_fileno)
os.close(stderr_save)
os.close(stderr_fd)


@contextmanager
def silence_logs():
LOGGER.disabled = True
yield
LOGGER.disabled = False
Loading
Loading