Skip to content

Commit ded08fc

Browse files
Merge pull request #3941 from mezotaken/master
Automated testing through API [WIP, feedback required]
2 parents 9484030 + ffc5b70 commit ded08fc

File tree

8 files changed

+192
-0
lines changed

8 files changed

+192
-0
lines changed

run_tests.bat

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@echo off
2+
set ERROR_REPORTING=FALSE
3+
set COMMANDLINE_ARGS= --api
4+
echo Launching SDWebUI...
5+
start "SDWebUITest" webui.bat
6+
7+
if not defined PYTHON (set PYTHON=python)
8+
if not defined VENV_DIR (set VENV_DIR=venv)
9+
set PYTHON="%~dp0%VENV_DIR%\Scripts\Python.exe"
10+
%PYTHON% test/server_poll.py
11+
for /f "tokens=2 delims=," %%a in ('tasklist /v /fo csv ^| findstr /i "SDWebUITest"') do set "$PID=%%a"
12+
13+
taskkill /PID %$PID% >nul 2>&1
14+
15+
pause

test/__init__.py

Whitespace-only changes.

test/extras_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
import requests
3+
from gradio.processing_utils import encode_pil_to_base64
4+
from PIL import Image
5+
6+
class TestExtrasWorking(unittest.TestCase):
7+
def setUp(self):
8+
self.url_img2img = "http://localhost:7860/sdapi/v1/extra-single-image"
9+
self.simple_extras = {
10+
"resize_mode": 0,
11+
"show_extras_results": True,
12+
"gfpgan_visibility": 0,
13+
"codeformer_visibility": 0,
14+
"codeformer_weight": 0,
15+
"upscaling_resize": 2,
16+
"upscaling_resize_w": 512,
17+
"upscaling_resize_h": 512,
18+
"upscaling_crop": True,
19+
"upscaler_1": "None",
20+
"upscaler_2": "None",
21+
"extras_upscaler_2_visibility": 0,
22+
"image": ""
23+
}
24+
25+
class TestExtrasCorrectness(unittest.TestCase):
26+
pass
27+
28+
if __name__ == "__main__":
29+
unittest.main()

test/img2img_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import unittest
2+
import requests
3+
from gradio.processing_utils import encode_pil_to_base64
4+
from PIL import Image
5+
6+
class TestImg2ImgWorking(unittest.TestCase):
7+
def setUp(self):
8+
self.url_img2img = "http://localhost:7860/sdapi/v1/img2img"
9+
self.simple_img2img = {
10+
"init_images": [
11+
encode_pil_to_base64(Image.open(r"test/test_files/img2img_basic.png"))
12+
],
13+
"resize_mode": 0,
14+
"denoising_strength": 0.75,
15+
"mask": None,
16+
"mask_blur": 4,
17+
"inpainting_fill": 0,
18+
"inpaint_full_res": False,
19+
"inpaint_full_res_padding": 0,
20+
"inpainting_mask_invert": 0,
21+
"prompt": "example prompt",
22+
"styles": [
23+
""
24+
],
25+
"seed": -1,
26+
"subseed": -1,
27+
"subseed_strength": 0,
28+
"seed_resize_from_h": -1,
29+
"seed_resize_from_w": -1,
30+
"batch_size": 1,
31+
"n_iter": 1,
32+
"steps": 3,
33+
"cfg_scale": 7,
34+
"width": 64,
35+
"height": 64,
36+
"restore_faces": False,
37+
"tiling": False,
38+
"negative_prompt": "",
39+
"eta": 0,
40+
"s_churn": 0,
41+
"s_tmax": 0,
42+
"s_tmin": 0,
43+
"s_noise": 1,
44+
"override_settings": {},
45+
"sampler_index": "Euler a",
46+
"include_init_images": False
47+
}
48+
def test_img2img_simple_performed(self):
49+
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)
50+
51+
def test_inpainting_masked_performed(self):
52+
self.simple_img2img["mask"] = encode_pil_to_base64(Image.open(r"test/test_files/mask_basic.png"))
53+
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)
54+
55+
class TestImg2ImgCorrectness(unittest.TestCase):
56+
pass
57+
58+
if __name__ == "__main__":
59+
unittest.main()

test/server_poll.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
import requests
3+
import time
4+
5+
timeout_threshold = 240
6+
start_time = time.time()
7+
while time.time()-start_time < timeout_threshold:
8+
try:
9+
requests.head("http://localhost:7860/")
10+
break
11+
except requests.exceptions.ConnectionError:
12+
pass
13+
if time.time()-start_time < timeout_threshold:
14+
suite = unittest.TestLoader().discover('', pattern='*_test.py')
15+
result = unittest.TextTestRunner(verbosity=2).run(suite)
16+
else:
17+
print("Launch unsuccessful")

test/test_files/img2img_basic.png

9.7 KB
Loading

test/test_files/mask_basic.png

362 Bytes
Loading

test/txt2img_test.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import unittest
2+
import requests
3+
4+
class TestTxt2ImgWorking(unittest.TestCase):
5+
def setUp(self):
6+
self.url_txt2img = "http://localhost:7860/sdapi/v1/txt2img"
7+
self.simple_txt2img = {
8+
"enable_hr": False,
9+
"denoising_strength": 0,
10+
"firstphase_width": 0,
11+
"firstphase_height": 0,
12+
"prompt": "example prompt",
13+
"styles": [
14+
""
15+
],
16+
"seed": -1,
17+
"subseed": -1,
18+
"subseed_strength": 0,
19+
"seed_resize_from_h": -1,
20+
"seed_resize_from_w": -1,
21+
"batch_size": 1,
22+
"n_iter": 1,
23+
"steps": 3,
24+
"cfg_scale": 7,
25+
"width": 64,
26+
"height": 64,
27+
"restore_faces": False,
28+
"tiling": False,
29+
"negative_prompt": "",
30+
"eta": 0,
31+
"s_churn": 0,
32+
"s_tmax": 0,
33+
"s_tmin": 0,
34+
"s_noise": 1,
35+
"sampler_index": "Euler a"
36+
}
37+
def test_txt2img_simple_performed(self):
38+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
39+
40+
def test_txt2img_with_negative_prompt_performed(self):
41+
self.simple_txt2img["negative_prompt"] = "example negative prompt"
42+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
43+
44+
def test_txt2img_not_square_image_performed(self):
45+
self.simple_txt2img["height"] = 128
46+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
47+
48+
def test_txt2img_with_hrfix_performed(self):
49+
self.simple_txt2img["enable_hr"] = True
50+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
51+
52+
def test_txt2img_with_restore_faces_performed(self):
53+
self.simple_txt2img["restore_faces"] = True
54+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
55+
56+
def test_txt2img_with_tiling_faces_performed(self):
57+
self.simple_txt2img["tiling"] = True
58+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
59+
60+
def test_txt2img_with_vanilla_sampler_performed(self):
61+
self.simple_txt2img["sampler_index"] = "PLMS"
62+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
63+
64+
def test_txt2img_multiple_batches_performed(self):
65+
self.simple_txt2img["n_iter"] = 2
66+
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
67+
68+
class TestTxt2ImgCorrectness(unittest.TestCase):
69+
pass
70+
71+
if __name__ == "__main__":
72+
unittest.main()

0 commit comments

Comments
 (0)