Skip to content

Commit

Permalink
Fix pytest and pygame-ce tests (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking13 authored Jul 31, 2024
1 parent fc3797d commit 5157757
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
6 changes: 6 additions & 0 deletions packages/pygame-ce/test_pygame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from pytest_pyodide import run_in_pyodide

from conftest import package_is_built


@pytest.fixture(scope="function")
def selenium_sdl(selenium_standalone):
Expand Down Expand Up @@ -34,6 +36,10 @@ def test_keyboard_input():
See: https://github.com/pyodide/pyodide/issues/4805#issuecomment-2169077347
TODO: find a better way to test keyboard input
"""

if not package_is_built("pygame-ce"):
pytest.skip("pygame-ce is not built")

from pathlib import Path

from auditwheel_emscripten import get_imports
Expand Down
52 changes: 37 additions & 15 deletions packages/pytest/test_pytest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
def test_pytest(selenium):
# TODO: don't use numpy in this test as it's not necessarily installed.
selenium.load_package(["pytest", "numpy"])
from pytest_pyodide import run_in_pyodide


@run_in_pyodide(packages=["pytest"])
def do_test(selenium, contents):
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path

import pytest

Path("test_pytest.py").write_text(contents)

selenium.run(
"""
from pathlib import Path
import os
import numpy
import pytest
out = StringIO()
with redirect_stdout(out):
result = pytest.main(["test_pytest.py"])

assert result == 1

out.seek(0)
output = out.read()
assert "2 passed" in output, output
assert "1 failed" in output, output
assert "1 warning" in output, output
assert "This is a warning" in output, output


def test_pytest(selenium):
contents = """
def test_success():
assert 1 == 1
base_dir = Path(numpy.__file__).parent / "core" / "tests"
"""
)
def test_warning():
import warnings
warnings.warn("This is a warning")
selenium.run("pytest.main([str(base_dir / 'test_api.py')])")
def test_fail():
assert 1 == 2
"""

logs = "\n".join(selenium.logs)
assert "INTERNALERROR" not in logs
do_test(selenium, contents)

0 comments on commit 5157757

Please sign in to comment.