Added pytests to makefile

This commit is contained in:
Caleb Herpin 2021-07-26 22:35:18 -05:00
parent 2f4b1aa3d4
commit 4f3f4daf65
9 changed files with 42 additions and 35 deletions

View File

@ -342,7 +342,7 @@ premade:
# TESTING
################################################################################
# This target runs Trick's Unit-tests and simulation-tests.
test: unit_test sim_test
test: unit_test sim_test pytest
@ echo "All tests completed sucessfully"
.PHONY: $(UNIT_TEST_DIRS)
@ -361,6 +361,9 @@ sim_test:
@ $(MAKE) -C test
@ $(MAKE) -C trick_sims test
pytest:
make -C share/trick/pymods/trick
#requirements:
# @ $(MAKE) -C trick_test/requirements_docs install

View File

@ -6,41 +6,22 @@ import subprocess
import inspect
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))), '../..')))
from utils import is_web_server_started, params
from trick.utils import is_web_server_started, params
# store history of failures per test class name and per index in parametrize (if parametrize used)
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {}
web_server_status = {}
# def pytest_runtest_makereport(item, call):
# if "incremental" in item.keywords:
# # incremental marker is used
# if call.excinfo is not None:
# # the test has failed
# # retrieve the class name of the test
# cls_name = str(item.cls)
# # retrieve the index of the test (if parametrize is used in combination with incremental)
# parametrize_index = (
# tuple(item.callspec.indices.values())
# if hasattr(item, "callspec")
# else ()
# )
# # retrieve the name of the test function
# test_name = item.originalname or item.name
# # store in _test_failed_incremental the original name of the failed test
# _test_failed_incremental.setdefault(cls_name, {}).setdefault(
# parametrize_index, test_name
# )
def pytest_runtest_setup(item):
if "webserver" in item.keywords:
#retrieve the class name of the test
cls_name = str(item.cls)
status = web_server_status.get(cls_name, None)
if status == None:
print(f"Building and starting sim for class {cls_name}")
build_sim()
web_server_status[cls_name] = is_web_server_started()
status = is_web_server_started()
web_server_status[cls_name] = status
print(f"Web server status for {cls_name} = {status}")
if not web_server_status[cls_name]:
pytest.fail("web server is not started.")
@ -58,8 +39,13 @@ web.server.port = {params.get_port()}
trick.var_server_set_port({params.get_var_server_port()})
exec(open("Modified_data/realtime.py").read())
exec(open("Modified_data/cannon.dr").read())""")
trick.frame_log_on()
trick.real_time_enable()
trick.exec_set_software_frame(0.1)
trick.itimer_enable()
trick.exec_set_enable_freeze(True)
trick.exec_set_freeze_command(True)""")
if params.get_build_sim():
build_cmd = f"echo \"cd {params.get_path_to_sim()} && make -C {params.get_trick_home()}/trick_source/web/CivetServer && make clean && {params.get_trick_home()}/bin/trick-CP\" | /bin/bash"
@ -70,6 +56,7 @@ exec(open("Modified_data/cannon.dr").read())""")
cmd = f'echo "cd {params.get_path_to_sim()} && ./S_main_Linux_9.3_x86_64.exe {os.path.join(params.get_input_folder(), params.get_test_input_file())} &" | /bin/bash'
print("....................Running:", cmd)
subprocess.run(cmd, shell=True)
os.system("/bin/bash")
@pytest.fixture(scope="session", autouse=True)
def close_sim():

View File

@ -0,0 +1,8 @@
PYTHON = python3
test: venv
venv/bin/python -m pytest tests/civet_server/test_http.py tests/civet_server/test_ws.py tests/gsetup
venv:
$(PYTHON) -m pip install virtualenv
$(PYTHON) -m virtualenv venv

View File

@ -0,0 +1,4 @@
websockets==9.1
pytest-asyncio==0.15.1
pytest==6.2.4
requests==2.26.0

View File

@ -9,6 +9,7 @@ from time import sleep
import shutil
import datetime
path.append(os.path.join(os.environ.get("TRICK_HOME", "../../../.."), "trick_source/trick_gsetup"))
from requests.api import get
# TODO: Get rid of this and use automatic discovery when Trick requires Python 2.7
@ -24,7 +25,7 @@ class TestWebserverHttp:
assert len(data["alloc_list"]) == 10
assert data["chunk_size"] == 10
assert data["chunk_start"] == 0
assert data["alloc_total"] == 49
assert data["alloc_total"] == 48
def test_alloc_info_2(self):
endpoint = "api/http/alloc_info?start=0&count=10"

View File

@ -3,7 +3,7 @@ from tkinter.constants import END
import os
import sys
sys.path.append(os.path.join(os.environ.get("TRICK_HOME", "../../../../../.."), "trick_source/trick_gsetup"))
sys.path.append(os.path.join(os.environ.get("TRICK_HOME", "../../../.."), "trick_source/trick_gsetup"))
from main import App, ChooseConfigure, LandingPage
def set_bool(bool, value):

View File

@ -2,11 +2,13 @@ import inspect
import os
import sys
import unittest
import pytest
# TODO: Get rid of this and use automatic discovery when Trick requires Python 2.7
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))), '..')))
from variable_server import *
# @pytest.mark.variableserver
class TestVariableServer(unittest.TestCase):
def setUp(self):

View File

@ -14,9 +14,9 @@ class Params:
# self.__ssl_cert_path = "server.pem"
# self.__ssl_cert_path = "/home/cherpin/git/trick_fork/trick_sims/Cannon/SIM_cannon_numeric/server.pem"
self.__ssl_cert_path = "/home/cherpin/.ssl/server.pem"
self.__build_sim = False
self.__build_sim = True
self.__start_sim = True
self.__trick_home = os.environ["TRICK_HOME"]
self.__trick_home = os.environ.get("TRICK_HOME", "../../../../")
self.__path_to_sim = os.path.join(self.get_trick_home(), "trick_sims", "Cannon", "SIM_cannon_numeric")
self.__input_folder = "RUN_test"
self.__test_input_file = f"tmp_input_for_test.py"
@ -69,10 +69,12 @@ class Params:
params = Params()
def is_web_server_started():
for _ in range(10): #Wait 10 seconds i.e 50 * .1 seconds
for _ in range(20): #Wait 2 seconds i.e 20 * .1 seconds
p = subprocess.run(f"echo \"netstat -tulpan | grep {params.get_port()}\" | /bin/bash", capture_output=True, shell=True)
# print(f"Checking for port output: {p.stdout}")
print(f"Checking for port output: {p.stdout}")
sleep(.1) #We sleep to use less recourses
if p.stdout != b"":
if "LISTEN" in p.stdout.decode():
print("Stdout is:", p.stdout)
os.system("/bin/bash")
return True
return False