This directory contains the comprehensive test suite for BasiCoCo, organized into unit tests and integration tests.
# Default run: everything except tests marked `slow` (~8 s)
python -m pytest
# Everything, including slow tests (Rubik's solver, live-server, pexpect audits; ~3.5 min)
python -m pytest -m ""
# Slow tests only
python -m pytest -m slow
# Run with coverage (see "Coverage" below)
python -m pytest -m "" --cov --timeout=300
# Run unit tests only
python -m pytest tests/unit/ -v
# Run integration tests only
python -m pytest tests/integration/ -v
# Run a specific test file
python -m pytest tests/unit/test_print_command.py -v
# Run tests matching a pattern
python -m pytest -k "test_for" -vTests individual components in isolation using pytest fixtures:
test_ast_parser.py- Abstract syntax tree parsingtest_command_*.py- Individual BASIC command implementationstest_expressions.py- Expression evaluationtest_functions.py- Built-in BASIC functionstest_variables.py- Variable managementtest_error_*.py- Error handling and context managementtest_graphics_commands.py- Graphics system componentstest_program_management_commands.py- DIR, FILES, DRIVE, SAVE, CLOAD, CSAVE commandstest_renum_command.py- RENUM line renumbering functionalitytest_on_commands.py- ON GOTO and ON GOSUB multi-way branchingtest_if_then_comprehensive.py- IF THEN statement variationstest_file_commands.py- File system command testingtest_regressions_executor.py,test_regressions_parser.py,test_regressions_builtins.py- regression tests for the Sept 2026 audit findings (each test names its task number)test_line_expansion.py- one-line IF/loop expansion (ast_converter.expand_statements)test_numeric_semantics.py- CoCo number formatting, VAL, operator precedence,^edge casestest_sandbox.py- filesystem sandbox, KILL protocol, per-session CDtest_comments_and_crunching.py-'/REM comments, block-IF detection, crunched spacing, ERR codestest_immediate_mode.py,test_chain_directive.py,test_renum_references.py,test_graphics_arguments.py,test_graphics_pixels.py,test_file_io_args.py,test_help_listing.py,test_app_sessions.pytest_robustness.py- awkward inputs across file I/O, ON ERROR, CHAIN ALL, loop stacks, strings and graphics; known bugs are strict xfails naming their TASKS.md entrytest_server_sessions.py- the web server's session handling via Flask-SocketIO's test client;test_audit_error_paths.py- error paths found by the coverage passtest_rubiks_*.py- Rubik's cube engine and solver (slow);test_rubiks_regressions.pysolves 100 seeded random scrambles and checks each solution in pycuber- And more...
Tests multiple components working together:
test_comprehensive_program.py- Complex program executiontest_cross_command_interactions.py- Command interaction testingtest_for_loops.py- Loop execution testingtest_program_execution_flow.py- Program flow controltest_state_isolation.py- State management between sessionstest_websocket_completion_signals.py- WebSocket communication (live server)test_websocket_security.py- origin checks, KILL protocol, Ctrl+C, command serialization, reconnect (live server)test_complex_if_then.py- Complex conditional logictest_program_audit.py- runs every bundled program via pexpect (in a temp working directory)test_standalone_cli.py-basicoco.pywith commands piped in, checking everything it prints on stdout and stderrtest_input_resume.py,test_array_combinations.py,test_single_line_control_structures.py
test_cli_sessions.py- drivescli_client.pywith pexpect against a live server: store/run, INPUT round trip, and a full lunar lander game
test_browser_client.py- the web client in headless Google Chrome (Playwright): commands typed into the real REPL, output and scrollback read back, graphics checked pixel by pixel on the real canvastest_browser_ui.py- Ctrl+C, INKEY$ key forwarding, the Copy button, the graphics info labels, PCLS with a color, SOUND note queueingtest_browser_reload.py- reloading the page reconnects to the same session with every tabtest_browser_tabs.py- switching tabs keeps each tab's program and variables (nothing is copied back to the server)- Shared helpers (
open_basic_page,wait_for,listing,add_tab,switch_to) are inbrowser/conftest.py - Marked
browserandslow; they use the installed Chrome (channel='chrome') and are skipped, with the launch error as the reason, if Chrome can't start. Run just these withpython -m pytest -m browser tests/client/dual_monitor_harness.js(run byunit/test_client_rendering.py) checks the same client code in Node on a fake canvas, with no browser needed
tests/integration/conftest.py provides live_server: it starts app.py on a free localhost port with its working directory set to a fresh temp directory, so anything the server writes stays out of the repo's programs/. Tests get live_server.url, .port and .programs_dir. A server that fails to start fails the test — nothing is silently skipped.
A test that takes longer than slow_test_budget (pytest.ini, 1 s) must be marked @pytest.mark.slow, which keeps it out of the default run. tests/plugins/slow_budget.py enforces this: an unmarked test over budget gets a PytestWarning in the run's summary naming it (the check is off while coverage traces). Tests are marked slow by a pytestmark line in the file, a marker on the class or test, or, for everything under integration/e2e/ and integration/cli/, by the path rules in the root conftest.py.
python -m pytest -m "" --cov --cov-report=json:coverage.json --timeout=300
python tools/diff_coverage.py coverage.json <rev> # uncovered lines changed since <rev>.coveragerc measures emulator/, app.py, cli_client.py and basicoco.py, including the server and CLI subprocesses the integration tests start (patch = subprocess; sigterm = true so the live server saves its data when stopped). Coverage slows the Rubik's tests past the default 30 s timeout, hence --timeout=300; a full run takes about 12 minutes.
Changed-but-uncovered lines left on purpose after the audit's coverage pass (#81), each with its reason:
app.py_Pretty.__repr__: only formats DEBUG log output.app.pydisconnect with no session / while a program runs: a timing race the socket tests can't set up deterministically.program_files.pyKILL'sPermissionError/OSError: OS failures after the sandbox check.graphics.pyDRAW's finalreturn []: fallthrough after every known DRAW command.program_executor.pyexception with an empty message: defensive.core.pynumeric INPUT overflow → 0:basic_number_prefixdoesn't raise OverflowError for any input found.
The pass also found bugs (#92–#95, fixed with tests) and dead branches (#96, deleted).
Tests use pytest with shared fixtures defined in conftest.py:
basicfixture — provides a freshCoCoBasicemulator instancegraphics_basicfixture — an emulator already in graphics modehelpersfixture — providesTestHelperswith utility methods for executing programs and checking output (load_program,run_to_completion,get_text_output,get_error_messages, ...)temp_programs_dirfixture — chdir into a temp directory with aprograms/subdirectory; any test that creates files must use it (never write to the realprograms/)
class TestMyCommand:
def test_basic_functionality(self, basic, helpers):
"""Test basic command functionality"""
result = basic.process_command('MY_COMMAND "TEST"')
text_outputs = helpers.get_text_output(result)
assert 'EXPECTED' in ' '.join(text_outputs)
def test_program_context(self, basic, helpers):
"""Test command in program context"""
program = [
'10 MY_COMMAND "HELLO"',
'20 END'
]
results = helpers.execute_program(basic, program)
text_outputs = helpers.get_text_output(results)
assert 'HELLO' in ' '.join(text_outputs)- Place unit tests in
unit/, integration tests inintegration/ - Use pytest fixtures (
basic,helpers) for emulator setup - Follow naming convention:
test_[component].pyfor files,test_[behavior]for methods - Include both positive and negative test cases
- Keep unit tests fast (< 100ms each)
- Each test should test one specific behavior
- Use descriptive assertion messages
- Avoid testing implementation details
- Focus on user-visible behavior