Skip to content

Commit 793a491

Browse files
committedMay 19, 2023
Overhaul tests to use py.test
1 parent d41a31a commit 793a491

17 files changed

+307
-326
lines changed
 

‎.github/workflows/run_tests.yaml

+42-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,53 @@ jobs:
1818
cache-dependency-path: |
1919
**/requirements*txt
2020
launch.py
21-
- name: Run tests
22-
run: python launch.py --tests test --no-half --disable-opt-split-attention --use-cpu all --skip-torch-cuda-test
21+
- name: Install test dependencies
22+
run: pip install wait-for-it -r requirements-test.txt
23+
env:
24+
PIP_DISABLE_PIP_VERSION_CHECK: "1"
25+
PIP_PROGRESS_BAR: "off"
26+
- name: Setup environment
27+
run: python launch.py --skip-torch-cuda-test --exit
2328
env:
2429
PIP_DISABLE_PIP_VERSION_CHECK: "1"
2530
PIP_PROGRESS_BAR: "off"
2631
TORCH_INDEX_URL: https://download.pytorch.org/whl/cpu
2732
WEBUI_LAUNCH_LIVE_OUTPUT: "1"
28-
- name: Upload main app stdout-stderr
33+
PYTHONUNBUFFERED: "1"
34+
- name: Start test server
35+
run: >
36+
python -m coverage run
37+
--data-file=.coverage.server
38+
launch.py
39+
--skip-prepare-environment
40+
--skip-torch-cuda-test
41+
--test-server
42+
--no-half
43+
--disable-opt-split-attention
44+
--use-cpu all
45+
--add-stop-route
46+
2>&1 | tee output.txt &
47+
- name: Run tests
48+
run: |
49+
wait-for-it --service 127.0.0.1:7860 -t 600
50+
python -m pytest -vv --junitxml=test/results.xml --cov . --cov-report=xml --verify-base-url test
51+
- name: Kill test server
52+
if: always()
53+
run: curl -vv -XPOST http://127.0.0.1:7860/_stop && sleep 10
54+
- name: Show coverage
55+
run: |
56+
python -m coverage combine .coverage*
57+
python -m coverage report -i
58+
python -m coverage html -i
59+
- name: Upload main app output
60+
uses: actions/upload-artifact@v3
61+
if: always()
62+
with:
63+
name: output
64+
path: output.txt
65+
- name: Upload coverage HTML
2966
uses: actions/upload-artifact@v3
3067
if: always()
3168
with:
32-
name: stdout-stderr
33-
path: |
34-
test/stdout.txt
35-
test/stderr.txt
69+
name: htmlcov
70+
path: htmlcov

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ notification.mp3
3535
/cache.json*
3636
/config_states/
3737
/node_modules
38-
/package-lock.json
38+
/package-lock.json
39+
/.coverage*

‎launch.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,8 @@ def prepare_environment():
310310
print("Exiting because of --exit argument")
311311
exit(0)
312312

313-
if args.tests and not args.no_tests:
314-
exitcode = tests(args.tests)
315-
exit(exitcode)
316313

317-
318-
def tests(test_dir):
314+
def configure_for_tests():
319315
if "--api" not in sys.argv:
320316
sys.argv.append("--api")
321317
if "--ckpt" not in sys.argv:
@@ -325,21 +321,8 @@ def tests(test_dir):
325321
sys.argv.append("--skip-torch-cuda-test")
326322
if "--disable-nan-check" not in sys.argv:
327323
sys.argv.append("--disable-nan-check")
328-
if "--no-tests" not in sys.argv:
329-
sys.argv.append("--no-tests")
330-
331-
print(f"Launching Web UI in another process for testing with arguments: {' '.join(sys.argv[1:])}")
332324

333325
os.environ['COMMANDLINE_ARGS'] = ""
334-
with open(os.path.join(script_path, 'test/stdout.txt'), "w", encoding="utf8") as stdout, open(os.path.join(script_path, 'test/stderr.txt'), "w", encoding="utf8") as stderr:
335-
proc = subprocess.Popen([sys.executable, *sys.argv], stdout=stdout, stderr=stderr)
336-
337-
import test.server_poll
338-
exitcode = test.server_poll.run_tests(proc, test_dir)
339-
340-
print(f"Stopping Web UI process with id {proc.pid}")
341-
proc.kill()
342-
return exitcode
343326

344327

345328
def start():
@@ -351,6 +334,15 @@ def start():
351334
webui.webui()
352335

353336

354-
if __name__ == "__main__":
355-
prepare_environment()
337+
def main():
338+
if not args.skip_prepare_environment:
339+
prepare_environment()
340+
341+
if args.test_server:
342+
configure_for_tests()
343+
356344
start()
345+
346+
347+
if __name__ == "__main__":
348+
main()

‎modules/cmd_args.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
parser.add_argument("--reinstall-xformers", action='store_true', help="launch.py argument: install the appropriate version of xformers even if you have some version already installed")
1313
parser.add_argument("--reinstall-torch", action='store_true', help="launch.py argument: install the appropriate version of torch even if you have some version already installed")
1414
parser.add_argument("--update-check", action='store_true', help="launch.py argument: chck for updates at startup")
15-
parser.add_argument("--tests", type=str, default=None, help="launch.py argument: run tests in the specified directory")
16-
parser.add_argument("--no-tests", action='store_true', help="launch.py argument: do not run tests even if --tests option is specified")
15+
parser.add_argument("--test-server", action='store_true', help="launch.py argument: configure server for testing")
16+
parser.add_argument("--skip-prepare-environment", action='store_true', help="launch.py argument: skip all environment preparation")
1717
parser.add_argument("--skip-install", action='store_true', help="launch.py argument: skip installation of packages")
1818
parser.add_argument("--data-dir", type=str, default=os.path.dirname(os.path.dirname(os.path.realpath(__file__))), help="base path where all user data is stored")
1919
parser.add_argument("--config", type=str, default=sd_default_config, help="path to config which constructs model",)

‎pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ ignore = [
3030
[tool.ruff.flake8-bugbear]
3131
# Allow default arguments like, e.g., `data: List[str] = fastapi.Query(None)`.
3232
extend-immutable-calls = ["fastapi.Depends", "fastapi.security.HTTPBasic"]
33+
34+
[tool.pytest.ini_options]
35+
base_url = "http://127.0.0.1:7860"

‎requirements-test.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pytest-base-url~=2.0
2+
pytest-cov~=4.0
3+
pytest~=7.3

‎test/basic_features/__init__.py

Whitespace-only changes.

‎test/basic_features/extras_test.py

-56
This file was deleted.

‎test/basic_features/img2img_test.py

-68
This file was deleted.

‎test/basic_features/txt2img_test.py

-82
This file was deleted.

‎test/basic_features/utils_test.py

-64
This file was deleted.

‎test/conftest.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
import pytest
4+
from PIL import Image
5+
from gradio.processing_utils import encode_pil_to_base64
6+
7+
test_files_path = os.path.dirname(__file__) + "/test_files"
8+
9+
10+
@pytest.fixture(scope="session") # session so we don't read this over and over
11+
def img2img_basic_image_base64() -> str:
12+
return encode_pil_to_base64(Image.open(os.path.join(test_files_path, "img2img_basic.png")))
13+
14+
15+
@pytest.fixture(scope="session") # session so we don't read this over and over
16+
def mask_basic_image_base64() -> str:
17+
return encode_pil_to_base64(Image.open(os.path.join(test_files_path, "mask_basic.png")))

‎test/server_poll.py

-26
This file was deleted.

‎test/test_extras.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import requests
2+
3+
4+
def test_simple_upscaling_performed(base_url, img2img_basic_image_base64):
5+
payload = {
6+
"resize_mode": 0,
7+
"show_extras_results": True,
8+
"gfpgan_visibility": 0,
9+
"codeformer_visibility": 0,
10+
"codeformer_weight": 0,
11+
"upscaling_resize": 2,
12+
"upscaling_resize_w": 128,
13+
"upscaling_resize_h": 128,
14+
"upscaling_crop": True,
15+
"upscaler_1": "Lanczos",
16+
"upscaler_2": "None",
17+
"extras_upscaler_2_visibility": 0,
18+
"image": img2img_basic_image_base64,
19+
}
20+
assert requests.post(f"{base_url}/sdapi/v1/extra-single-image", json=payload).status_code == 200
21+
22+
23+
def test_png_info_performed(base_url, img2img_basic_image_base64):
24+
payload = {
25+
"image": img2img_basic_image_base64,
26+
}
27+
assert requests.post(f"{base_url}/sdapi/v1/extra-single-image", json=payload).status_code == 200
28+
29+
30+
def test_interrogate_performed(base_url, img2img_basic_image_base64):
31+
payload = {
32+
"image": img2img_basic_image_base64,
33+
"model": "clip",
34+
}
35+
assert requests.post(f"{base_url}/sdapi/v1/extra-single-image", json=payload).status_code == 200

‎test/test_img2img.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
import pytest
3+
import requests
4+
5+
6+
@pytest.fixture()
7+
def url_img2img(base_url):
8+
return f"{base_url}/sdapi/v1/img2img"
9+
10+
11+
@pytest.fixture()
12+
def simple_img2img_request(img2img_basic_image_base64):
13+
return {
14+
"batch_size": 1,
15+
"cfg_scale": 7,
16+
"denoising_strength": 0.75,
17+
"eta": 0,
18+
"height": 64,
19+
"include_init_images": False,
20+
"init_images": [img2img_basic_image_base64],
21+
"inpaint_full_res": False,
22+
"inpaint_full_res_padding": 0,
23+
"inpainting_fill": 0,
24+
"inpainting_mask_invert": False,
25+
"mask": None,
26+
"mask_blur": 4,
27+
"n_iter": 1,
28+
"negative_prompt": "",
29+
"override_settings": {},
30+
"prompt": "example prompt",
31+
"resize_mode": 0,
32+
"restore_faces": False,
33+
"s_churn": 0,
34+
"s_noise": 1,
35+
"s_tmax": 0,
36+
"s_tmin": 0,
37+
"sampler_index": "Euler a",
38+
"seed": -1,
39+
"seed_resize_from_h": -1,
40+
"seed_resize_from_w": -1,
41+
"steps": 3,
42+
"styles": [],
43+
"subseed": -1,
44+
"subseed_strength": 0,
45+
"tiling": False,
46+
"width": 64,
47+
}
48+
49+
50+
def test_img2img_simple_performed(url_img2img, simple_img2img_request):
51+
assert requests.post(url_img2img, json=simple_img2img_request).status_code == 200
52+
53+
54+
def test_inpainting_masked_performed(url_img2img, simple_img2img_request, mask_basic_image_base64):
55+
simple_img2img_request["mask"] = mask_basic_image_base64
56+
assert requests.post(url_img2img, json=simple_img2img_request).status_code == 200
57+
58+
59+
def test_inpainting_with_inverted_masked_performed(url_img2img, simple_img2img_request, mask_basic_image_base64):
60+
simple_img2img_request["mask"] = mask_basic_image_base64
61+
simple_img2img_request["inpainting_mask_invert"] = True
62+
assert requests.post(url_img2img, json=simple_img2img_request).status_code == 200
63+
64+
65+
def test_img2img_sd_upscale_performed(url_img2img, simple_img2img_request):
66+
simple_img2img_request["script_name"] = "sd upscale"
67+
simple_img2img_request["script_args"] = ["", 8, "Lanczos", 2.0]
68+
assert requests.post(url_img2img, json=simple_img2img_request).status_code == 200

‎test/test_txt2img.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
import pytest
3+
import requests
4+
5+
6+
@pytest.fixture()
7+
def url_txt2img(base_url):
8+
return f"{base_url}/sdapi/v1/txt2img"
9+
10+
11+
@pytest.fixture()
12+
def simple_txt2img_request():
13+
return {
14+
"batch_size": 1,
15+
"cfg_scale": 7,
16+
"denoising_strength": 0,
17+
"enable_hr": False,
18+
"eta": 0,
19+
"firstphase_height": 0,
20+
"firstphase_width": 0,
21+
"height": 64,
22+
"n_iter": 1,
23+
"negative_prompt": "",
24+
"prompt": "example prompt",
25+
"restore_faces": False,
26+
"s_churn": 0,
27+
"s_noise": 1,
28+
"s_tmax": 0,
29+
"s_tmin": 0,
30+
"sampler_index": "Euler a",
31+
"seed": -1,
32+
"seed_resize_from_h": -1,
33+
"seed_resize_from_w": -1,
34+
"steps": 3,
35+
"styles": [],
36+
"subseed": -1,
37+
"subseed_strength": 0,
38+
"tiling": False,
39+
"width": 64,
40+
}
41+
42+
43+
def test_txt2img_simple_performed(url_txt2img, simple_txt2img_request):
44+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
45+
46+
47+
def test_txt2img_with_negative_prompt_performed(url_txt2img, simple_txt2img_request):
48+
simple_txt2img_request["negative_prompt"] = "example negative prompt"
49+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
50+
51+
52+
def test_txt2img_with_complex_prompt_performed(url_txt2img, simple_txt2img_request):
53+
simple_txt2img_request["prompt"] = "((emphasis)), (emphasis1:1.1), [to:1], [from::2], [from:to:0.3], [alt|alt1]"
54+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
55+
56+
57+
def test_txt2img_not_square_image_performed(url_txt2img, simple_txt2img_request):
58+
simple_txt2img_request["height"] = 128
59+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
60+
61+
62+
def test_txt2img_with_hrfix_performed(url_txt2img, simple_txt2img_request):
63+
simple_txt2img_request["enable_hr"] = True
64+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
65+
66+
67+
def test_txt2img_with_tiling_performed(url_txt2img, simple_txt2img_request):
68+
simple_txt2img_request["tiling"] = True
69+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
70+
71+
72+
def test_txt2img_with_restore_faces_performed(url_txt2img, simple_txt2img_request):
73+
simple_txt2img_request["restore_faces"] = True
74+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
75+
76+
77+
@pytest.mark.parametrize("sampler", ["PLMS", "DDIM", "UniPC"])
78+
def test_txt2img_with_vanilla_sampler_performed(url_txt2img, simple_txt2img_request, sampler):
79+
simple_txt2img_request["sampler_index"] = sampler
80+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
81+
82+
83+
def test_txt2img_multiple_batches_performed(url_txt2img, simple_txt2img_request):
84+
simple_txt2img_request["n_iter"] = 2
85+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200
86+
87+
88+
def test_txt2img_batch_performed(url_txt2img, simple_txt2img_request):
89+
simple_txt2img_request["batch_size"] = 2
90+
assert requests.post(url_txt2img, json=simple_txt2img_request).status_code == 200

‎test/test_utils.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
import requests
3+
4+
5+
def test_options_write(base_url):
6+
url_options = f"{base_url}/sdapi/v1/options"
7+
response = requests.get(url_options)
8+
assert response.status_code == 200
9+
10+
pre_value = response.json()["send_seed"]
11+
12+
assert requests.post(url_options, json={'send_seed': (not pre_value)}).status_code == 200
13+
14+
response = requests.get(url_options)
15+
assert response.status_code == 200
16+
assert response.json()['send_seed'] == (not pre_value)
17+
18+
requests.post(url_options, json={"send_seed": pre_value})
19+
20+
21+
@pytest.mark.parametrize("url", [
22+
"sdapi/v1/cmd-flags",
23+
"sdapi/v1/samplers",
24+
"sdapi/v1/upscalers",
25+
"sdapi/v1/sd-models",
26+
"sdapi/v1/hypernetworks",
27+
"sdapi/v1/face-restorers",
28+
"sdapi/v1/realesrgan-models",
29+
"sdapi/v1/prompt-styles",
30+
"sdapi/v1/embeddings",
31+
])
32+
def test_get_api_url(base_url, url):
33+
assert requests.get(f"{base_url}/{url}").status_code == 200

0 commit comments

Comments
 (0)
Please sign in to comment.