Remote running of PyTest tests
Introduction
In this article you can get acquainted with various examples of remote running of PyTest tests.
FastAPI and subprocess
Consider the following setup: two machines with Windows 7. We need to run the tests on machine W01 from machine W02.
On both machines I recommend creating and
activating
a
virtual environment
,
for example with
venv
.
python -m venv venv
.\venv\Scripts\activate
Machine 1, on which the tests will be run
python -m pip install fastapi uvicorn
# HOST W01 FastAPI api_demo.py import subprocess import sys import uvicorn from fastapi import FastAPI app = FastAPI() python_venv = sys.prefix + "\" + "Scripts\python.exe" @app.get("/test") def test(): subprocess.run([ python_venv, "-m" "pytest", "--maxfail=1", "--disable-warnings", "-vv", "--tb=long", "--html=report.html", "--self-contained-html", "test_smoke.py" ]) if __name__ == "__main__": uvicorn.run("api_demo:app", host="0.0.0.0", port=5000, log_level="info")
Machine 1, the tests. Replace them with your own and install the required libraries.
# HOST W01 Tests smoke_test.py import pytest def test_pass(): assert 1 == 1 def test_fail(): assert 1 == 2
Machine 2, from which the signal to run the tests is sent
python -m pip install requests
# HOST W02 trigger.py import requests W01_IP = "10.8.1.101:5000" r = requests.get(f"http://{W01_IP}/test") print(r.status_code)
On the first machine, port 5000 must be opened.
Open a port in Windows via PowerShell
Open a port in Windows manually
Then FastAPI must be started
python .\api_demo.py
←[32mINFO←[0m: Started server process [←[36m2156←[0m] ←[32mINFO←[0m: Waiting for application startup. ←[32mINFO←[0m: Application startup complete. ←[32mINFO←[0m: Uvicorn running on ←[1mhttp://0.0.0.0:5000←[0m (Press CTRL+C to quit)
On the second machine, the script must be run
python .\trigger.py
200
Article author: Arthur Isaev