Skip to content

Instantly share code, notes, and snippets.

@helloworld
Created December 8, 2022 17:05
Show Gist options
  • Save helloworld/b41501b4a26229024e2842d1c1729bf2 to your computer and use it in GitHub Desktop.
Save helloworld/b41501b4a26229024e2842d1c1729bf2 to your computer and use it in GitHub Desktop.

Revisions

  1. helloworld created this gist Dec 8, 2022.
    55 changes: 55 additions & 0 deletions modal_pytest.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import modal

    pytest_image = modal.Image.debian_slim().pip_install(["pytest"])
    stub = modal.Stub()

    code = """
    def hello_world(name=None, age=None, city=None):
    return 'Hello World!'
    def test_hello_world():
    assert hello_world() == 'Hello World!'
    def test_hello_world_with_name():
    assert hello_world('John') == 'Hello John!'
    assert hello_world(name='John') == 'Hello John!'
    def test_hello_world_with_name_and_age():
    assert hello_world('John', 30) == 'Hello John! You are 30 years old.'
    assert hello_world(
    name='John', age=30) == 'Hello John! You are 30 years old.'
    def test_hello_world_with_name_and_age_and_city():
    assert hello_world(
    'John', 30, 'New York') == 'Hello John! You are 30 years old. You are from New York.'
    assert hello_world(
    name='John', age=30, city='New York') == 'Hello John! You are 30 years old. You are from New York.'
    """


    @stub.function(image=pytest_image, shared_volumes={"/root/pytest": modal.SharedVolume()})
    def pytest_runner():
    import tempfile
    import subprocess

    with tempfile.TemporaryDirectory() as dirname:
    with open(f"{dirname}/test.py", "w") as f:
    f.write(code)

    try:
    output = subprocess.check_output(
    ["pytest", "test.py"], cwd=dirname, stderr=subprocess.STDOUT
    ).decode("utf-8")
    except subprocess.CalledProcessError as e:
    output = e.output.decode("utf-8")

    print(output)


    if __name__ == "__main__":
    with stub.run():
    pytest_runner()