Skip to main content

Create tests for ejudge and co

Project description

Create tests for ejudge using solution

Install

pip install git+https://github.com/ShashkovS/ej_test_gen.git --user --upgrade

Example:

# sol.py
n = int(input())
fct = 1
for i in range(2, n + 1):
    fct *= i
print(fct)
# test_creator.py
from ej_test_gen import TestRunner, random
runner = TestRunner(solution="sol.py", tests_dir="tests")
# runner = TestRunner(solution="sol.cpp", tests_dir="tests", use_WSL=True)

runner.test("""3""")
runner.test("""5""")

for tests_in_group, group_max in [(2, 10), (5, 50)]:
    for __ in range(tests_in_group):
        n = random.randint(0, group_max)
        test = f'{n}'
        runner.test(test)
> python test_creator.py
001: 3         -->    6                                           Done! 0.27c
002: 2         -->    2                                           Done! 0.25c
003: 3         -->    6                                           Done! 0.26c
004: 31        -->    8222838654177922817725562880000000          Done! 0.25c
005: 10        -->    3628800                                     Done! 0.26c
006: 15        -->    1307674368000                               Done! 0.26c
007: 41        -->    3345252661316380710817006205344075166515     Done! 0.27c
008: 15        -->    1307674368000                               Done! 0.27c

TestRunner resolves relative solution and tests_dir paths from the directory of the script where TestRunner is created. This means sol.py and test_creator.py can live in the same directory, and generated tests will be written to tests/ next to them even if you run the script from another directory:

python path/to/test_creator.py

Pass working_dir explicitly when you want a different base directory. For example, working_dir="." keeps the old behavior where relative paths are resolved from the process current working directory.

Header and footer

Use header_file and footer_file when generated tests should run a solution with extra code prepended or appended. Both paths are optional and are resolved relative to working_dir, just like solution.

runner = TestRunner(
    solution="sol.py",
    header_file="header.py",
    footer_file="footer.py",
    tests_dir="tests",
)

Before each Python run, or before C++ compilation, TestRunner builds a temporary source file:

header contents
solution contents
footer contents

The temporary file is removed after use.

Solution errors

Use on_error to choose what happens when the solution exits with a non-zero return code or writes to stderr:

# default: stop generation and raise RuntimeError
runner = TestRunner(solution="sol.py", tests_dir="tests")

# ignore: skip failed tests and keep generating the next ones
runner = TestRunner(solution="sol.py", tests_dir="tests", on_error="ignore")

# output: write stderr/traceback to the answer file
runner = TestRunner(solution="sol.py", tests_dir="tests", on_error="output")

Old names are still accepted for compatibility: on_error="raise" means on_error="default", and on_error="skip" means on_error="ignore".

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ej_test_gen-0.4.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ej_test_gen-0.4.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file ej_test_gen-0.4.0.tar.gz.

File metadata

  • Download URL: ej_test_gen-0.4.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ej_test_gen-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c5dbc9f3ea3c4ba49e43d4b3b4d31b0a8369c7af4bfdaeeac3729f9ad9ebf1a1
MD5 c850b57026de311d225047e8b72abf84
BLAKE2b-256 978022be345b77772240a3d8c793dd1b7a137feb9c6d769a56535049109a9fc2

See more details on using hashes here.

File details

Details for the file ej_test_gen-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: ej_test_gen-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ej_test_gen-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6b406b51c931e2287ac826f76f9ac5d6e0b8affd5ac873bf6b9de74a333ec61
MD5 46e0039e5edf5a4dc97cc5dec89dd9cc
BLAKE2b-256 2d7ebff6bfe7280056a1f45a1f5aa10c16cdd18a7bb84bcb59411e8792fe9191

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page