Skip to content

Commit af45b5a

Browse files
committed
Testing with API added
1 parent 35c45df commit af45b5a

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-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/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/txt2img_test.py

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

0 commit comments

Comments
 (0)