{"id":21206214,"url":"https://github.com/rtmigo/runwerk_py","last_synced_at":"2025-03-14T23:11:50.725Z","repository":{"id":138533406,"uuid":"360938256","full_name":"rtmigo/runwerk_py","owner":"rtmigo","description":"Python package running local Flask server process in the background","archived":false,"fork":false,"pushed_at":"2021-05-21T14:44:15.000Z","size":82,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"staging","last_synced_at":"2025-01-21T15:32:13.627Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rtmigo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-23T16:05:06.000Z","updated_at":"2021-05-21T14:42:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"e74e505e-5f82-45e5-9163-26ee13a8b5ba","html_url":"https://github.com/rtmigo/runwerk_py","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Frunwerk_py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Frunwerk_py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Frunwerk_py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtmigo%2Frunwerk_py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rtmigo","download_url":"https://codeload.github.com/rtmigo/runwerk_py/tar.gz/refs/heads/staging","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243658274,"owners_count":20326467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T20:54:50.546Z","updated_at":"2025-03-14T23:11:50.712Z","avatar_url":"https://github.com/rtmigo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [runwerk](https://github.com/rtmigo/runwerk_py#readme)\n\nPython package running child Werkzeug server process in the background.\n\nThis allows you to launch a web application and send a request to it.\n\n``` python3\nwith RunWerk(module=\"myapp.main\"):\n    response = requests.get('http://127.0.0.1:5000/hi')\n```\n\n\nThis is useful for testing web applications. In particular, Flask applications.\n\nRunwerk supports Python 3.7+ on Linux and macOS.\n\n--------------------------------------------------------------------------------\n\nI prefer to **test** my own **Flask app** like a **black box**. I want to access\nonly the public HTTP API the server provides. So I can test both local and\nremote servers the same way.\n\n``` python\ntest_my_api('http://127.0.0.1:5000')\ntest_my_api('http://deployed-on-remote-server.net')\n```\n\nI also want to easily restart the local server process. This way, I can be sure\nthat after a restart, the global variables inside the server have their default\nvalues.\n\nI could _manually_ start the local Flask+Werkzeug server in a terminal window\nand get a working API at 127.0.0.1:5000. But I want this to be done \n_automatically_, since the tests are automated.\n\nThe `RunWerk` object starts the local server in parallel process and keeps it\nrunning.\n\nThe same effect could be achieved by launching standard Flask application in a\nterminal:\n\n``` bash\n$ python3 /my/flask-app/main.py\n```\n\n``` text\n * Serving Flask app \"server\" (lazy loading)\n * Environment: production\n   WARNING: This is a development server. Do not use it in a production deployment.\n   Use a production WSGI server instead.\n * Debug mode: off\n * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n```\n\nThe `RunWerk` does the same silently, without terminal window.\n\n``` python\nwith RunWerk(command=['python3', '/path/flask_app/main.py']):\n  # the server was started and initialized.\n  # It is now running on http://127.0.0.1:5000/\n  # No need for Ctrl+C. Get out of `with` and the server stops\n  pass\n```\n\n# Install\n\n``` bash\n$ pip install git+https://github.com/rtmigo/runwerk_py#egg=runwerk\n```\n\n# Use\n\nWe assume, your `main.py` contains something like\n\n``` python3\nfrom flask import Flask\n\napp = Flask(__name__)\n\n@app.route('/status')\ndef status():\n    return 'OK'\n    \n@app.route('/answer')\ndef answer():\n    return '42'\n    \nif __name__ == \"__main__\":\n    app.run()\n```\n\nThen you can run tests like this:\n\n``` python3\nimport requests\nfrom runwerk import RunWerk\n\n# the server is not running  \n\nwith RunWerk([\"python3\", \"/path_to/flask_app/main.py\"]):\n\n    # we have just started main.py\n        \n    # So the server is running, and you can send requests \n    # directly to localhost. By default the Flask server listens  \n    # to port 5000\n    \n    assert requests.get('http://127.0.0.1:5000/status') == 'OK'\n    assert requests.get('http://127.0.0.1:5000/answer') == '42'\n    \n# the server is not running again     \n```\n\n## Creating RunWerk\n\n### With module name\n\n``` python3 \nwith RunWerk(module=\"main\"):\n    pass\n```\n\n``` python3 \nwith RunWerk(module=\"flask_app.main\"):\n    pass\n```\n\n### With command line\n\n``` python3 \nwith RunWerk([\"python3\", \"/path_to/flask_app/main.py\"]):\n    pass\n```\n\nTo run command with the current interpreter (`sys.executable`), you can set the\nfirst item of `command` to `None`.\n\n``` python3 \nwith RunWerk([None, \"/path_to/flask_app/main.py\"]):\n    pass\n```\n\n## Temporary disabling RunWerk\n\nWhen `$RUNWERK_ENABLED` environment variable is set to `False`, the server will\nnot be started. The code runs as usual, but `RunWerk` object does nothing.\n\n``` bash\nRUNWERK_ENABLED=False python3 run_my_unittest.py\n```\n\nThis is useful when you're running the server manually, for example, when\ndebugging.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtmigo%2Frunwerk_py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frtmigo%2Frunwerk_py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtmigo%2Frunwerk_py/lists"}