Skip to content

Commit 59dfe08

Browse files
committed
launch tests from launch.py with --tests commandline argument
1 parent 05a657d commit 59dfe08

File tree

7 files changed

+50
-40
lines changed

7 files changed

+50
-40
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ notification.mp3
2929
/textual_inversion
3030
.vscode
3131
/extensions
32+
/test/stdout.txt
33+
/test/stderr.txt

launch.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ def prepare_enviroment():
128128
blip_commit_hash = os.environ.get('BLIP_COMMIT_HASH', "48211a1594f1321b00f14c9f7a5b4813144b2fb9")
129129

130130
sys.argv += shlex.split(commandline_args)
131+
test_argv = [x for x in sys.argv if x != '--tests']
131132

132133
sys.argv, skip_torch_cuda_test = extract_arg(sys.argv, '--skip-torch-cuda-test')
133134
sys.argv, reinstall_xformers = extract_arg(sys.argv, '--reinstall-xformers')
134135
sys.argv, update_check = extract_arg(sys.argv, '--update-check')
136+
sys.argv, run_tests = extract_arg(sys.argv, '--tests')
135137
xformers = '--xformers' in sys.argv
136138
deepdanbooru = '--deepdanbooru' in sys.argv
137139
ngrok = '--ngrok' in sys.argv
@@ -194,6 +196,23 @@ def prepare_enviroment():
194196
print("Exiting because of --exit argument")
195197
exit(0)
196198

199+
if run_tests:
200+
tests(test_argv)
201+
exit(0)
202+
203+
204+
def tests(argv):
205+
print(f"Launching Web UI in another process for testing with arguments: {' '.join(argv[1:])}")
206+
207+
with open('test/stdout.txt', "w", encoding="utf8") as stdout, open('test/stderr.txt', "w", encoding="utf8") as stderr:
208+
proc = subprocess.Popen([sys.executable, *argv], stdout=stdout, stderr=stderr)
209+
210+
import test.server_poll
211+
test.server_poll.run_tests()
212+
213+
print(f"Stopping Web UI process with id {proc.pid}")
214+
proc.kill()
215+
197216

198217
def start_webui():
199218
print(f"Launching Web UI with arguments: {' '.join(sys.argv[1:])}")

run_tests.bat

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/extras_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import unittest
2-
import requests
3-
from gradio.processing_utils import encode_pil_to_base64
4-
from PIL import Image
2+
53

64
class TestExtrasWorking(unittest.TestCase):
75
def setUp(self):
@@ -22,8 +20,10 @@ def setUp(self):
2220
"image": ""
2321
}
2422

23+
2524
class TestExtrasCorrectness(unittest.TestCase):
2625
pass
2726

27+
2828
if __name__ == "__main__":
2929
unittest.main()

test/img2img_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
from gradio.processing_utils import encode_pil_to_base64
44
from PIL import Image
55

6+
67
class TestImg2ImgWorking(unittest.TestCase):
78
def setUp(self):
89
self.url_img2img = "http://localhost:7860/sdapi/v1/img2img"
910
self.simple_img2img = {
10-
"init_images": [
11-
encode_pil_to_base64(Image.open(r"test/test_files/img2img_basic.png"))
12-
],
11+
"init_images": [encode_pil_to_base64(Image.open(r"test/test_files/img2img_basic.png"))],
1312
"resize_mode": 0,
1413
"denoising_strength": 0.75,
1514
"mask": None,
@@ -19,9 +18,7 @@ def setUp(self):
1918
"inpaint_full_res_padding": 0,
2019
"inpainting_mask_invert": 0,
2120
"prompt": "example prompt",
22-
"styles": [
23-
""
24-
],
21+
"styles": [],
2522
"seed": -1,
2623
"subseed": -1,
2724
"subseed_strength": 0,
@@ -45,15 +42,18 @@ def setUp(self):
4542
"sampler_index": "Euler a",
4643
"include_init_images": False
4744
}
45+
4846
def test_img2img_simple_performed(self):
4947
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)
5048

5149
def test_inpainting_masked_performed(self):
5250
self.simple_img2img["mask"] = encode_pil_to_base64(Image.open(r"test/test_files/mask_basic.png"))
5351
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)
5452

53+
5554
class TestImg2ImgCorrectness(unittest.TestCase):
5655
pass
5756

57+
5858
if __name__ == "__main__":
5959
unittest.main()

test/server_poll.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
import requests
33
import time
44

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")
5+
6+
def run_tests():
7+
timeout_threshold = 240
8+
start_time = time.time()
9+
while time.time()-start_time < timeout_threshold:
10+
try:
11+
requests.head("http://localhost:7860/")
12+
break
13+
except requests.exceptions.ConnectionError:
14+
pass
15+
if time.time()-start_time < timeout_threshold:
16+
suite = unittest.TestLoader().discover('', pattern='*_test.py')
17+
result = unittest.TextTestRunner(verbosity=2).run(suite)
18+
else:
19+
print("Launch unsuccessful")

test/txt2img_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import requests
33

4+
45
class TestTxt2ImgWorking(unittest.TestCase):
56
def setUp(self):
67
self.url_txt2img = "http://localhost:7860/sdapi/v1/txt2img"
@@ -10,9 +11,7 @@ def setUp(self):
1011
"firstphase_width": 0,
1112
"firstphase_height": 0,
1213
"prompt": "example prompt",
13-
"styles": [
14-
""
15-
],
14+
"styles": [],
1615
"seed": -1,
1716
"subseed": -1,
1817
"subseed_strength": 0,
@@ -34,6 +33,7 @@ def setUp(self):
3433
"s_noise": 1,
3534
"sampler_index": "Euler a"
3635
}
36+
3737
def test_txt2img_simple_performed(self):
3838
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
3939

@@ -65,8 +65,10 @@ def test_txt2img_multiple_batches_performed(self):
6565
self.simple_txt2img["n_iter"] = 2
6666
self.assertEqual(requests.post(self.url_txt2img, json=self.simple_txt2img).status_code, 200)
6767

68+
6869
class TestTxt2ImgCorrectness(unittest.TestCase):
6970
pass
7071

72+
7173
if __name__ == "__main__":
7274
unittest.main()

0 commit comments

Comments
 (0)