Skip to content

Commit 32a2c8e

Browse files
committed
add tests for cmd output
1 parent ad12386 commit 32a2c8e

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

tests/test_api/test_empty_output/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
from hyperactive import Hyperactive
3+
4+
5+
class Experiment(BaseExperiment):
6+
def objective_function(self, opt):
7+
score = -opt["x1"] * opt["x1"]
8+
return score
9+
10+
11+
experiment = Experiment()
12+
13+
search_config = SearchConfig(
14+
x1=list(np.arange(-100, 101, 1)),
15+
)
16+
17+
18+
hyper = Hyperactive()
19+
hyper.add_search(experiment, search_config, n_iter=30, memory=True)
20+
hyper.run(verbosity=False)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os, sys, subprocess
2+
3+
here = os.path.dirname(os.path.abspath(__file__))
4+
5+
verbose_file = os.path.join(here, "verbose.py")
6+
non_verbose_file = os.path.join(here, "non_verbose.py")
7+
8+
9+
def _run_subprocess(script):
10+
output = []
11+
process = subprocess.Popen(
12+
[sys.executable, "-u", script],
13+
stdout=subprocess.PIPE,
14+
stderr=subprocess.PIPE,
15+
text=True,
16+
bufsize=1, # Line buffered
17+
env={**os.environ, "PYTHONUNBUFFERED": "1"},
18+
)
19+
# Read output line by line
20+
while True:
21+
line = process.stdout.readline()
22+
if line:
23+
output.append(line)
24+
if not line and process.poll() is not None:
25+
break
26+
27+
return "".join(output), process.stderr.read()
28+
29+
30+
def test_empty_output():
31+
stdout_verb, stderr_verb = _run_subprocess(verbose_file)
32+
stdout_non_verb, stderr_non_verb = _run_subprocess(non_verbose_file)
33+
34+
print("\n stdout_verb \n", stdout_verb, "\n")
35+
print("\n stderr_verb \n", stderr_verb, "\n")
36+
37+
print("\n stdout_non_verb \n", stdout_non_verb, "\n")
38+
print("\n stderr_non_verb \n", stderr_non_verb, "\n")
39+
40+
assert "Results:" in stdout_verb
41+
assert not stdout_non_verb
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
import numpy as np
3+
4+
from hyperactive.optimizers import HillClimbingOptimizer
5+
from hyperactive.experiment import BaseExperiment
6+
from hyperactive.search_config import SearchConfig
7+
8+
9+
class Experiment(BaseExperiment):
10+
def objective_function(self, opt):
11+
score = -opt["x1"] * opt["x1"]
12+
return score
13+
14+
15+
experiment = Experiment()
16+
17+
search_config = SearchConfig(
18+
x1=list(np.arange(-100, 101, 1)),
19+
)
20+
21+
22+
hyper = HillClimbingOptimizer()
23+
hyper.add_search(experiment, search_config, n_iter=30, memory=True)
24+
hyper.run()
25+
26+
sys.stdout.flush()

0 commit comments

Comments
 (0)