Back to Repositories

Testing Tic-tac-toe Game Logic in AutoGPT

This test suite implements comprehensive unit testing for a Tic-tac-toe game implementation, validating game outcomes including wins, losses, and draws. The tests utilize pytest’s parametrization feature to verify multiple game scenarios through automated input simulation and output validation.

Test Coverage Overview

The test suite provides thorough coverage of core Tic-tac-toe game functionality, including:

  • Player 1 winning scenarios
  • Player 2 winning scenarios
  • Draw game conditions
  • Input/output handling through subprocess
Key edge cases are validated through parameterized test data, ensuring game state transitions and win detection logic work correctly.

Implementation Analysis

The testing approach leverages pytest’s parametrize decorator to implement data-driven testing patterns. Each test case simulates a complete game by feeding move coordinates through a subprocess interface, allowing verification of the game’s input handling and outcome determination.

The implementation uses subprocess.Popen for process isolation and communication, ensuring robust testing of the game’s I/O behavior.

Technical Details

Testing tools and components:

  • pytest framework for test organization and execution
  • subprocess module for game process management
  • Parameterized test data for multiple game scenarios
  • Standard input/output stream manipulation

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Isolation of test cases through process separation
  • Data-driven test design using parametrization
  • Clear test case organization and naming
  • Comprehensive coverage of game outcomes
  • Robust input/output handling and validation

significant-gravitas/autogpt

classic/benchmark/agbenchmark/challenges/verticals/code/5_tic_tac_toe/custom_python/test.py

            
import subprocess

import pytest


def run_game_with_inputs(inputs):
    # Start the game process
    process = subprocess.Popen(
        ["python", "tic_tac_toe.py"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )

    # Send the input moves one by one
    output, errors = process.communicate("
".join(inputs))

    # Print the inputs and outputs
    print("Inputs:
", "
".join(inputs))
    print("Output:
", output)
    print("Errors:
", errors)

    return output


@pytest.mark.parametrize(
    "inputs, expected_output",
    [
        (["0,0", "1,0", "0,1", "1,1", "0,2"], "Player 1 won!"),
        (["1,0", "0,0", "1,1", "0,1", "2,0", "0,2"], "Player 2 won!"),
        (["0,0", "0,1", "0,2", "1,1", "1,0", "1,2", "2,1", "2,0", "2,2"], "Draw"),
    ],
)
def test_game(inputs, expected_output):
    output = run_game_with_inputs(inputs)
    assert expected_output in output


if __name__ == "__main__":
    pytest.main([__file__])