-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Travis Collins <[email protected]>
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"""pytest plugin for pybenc""" | ||
import os | ||
import yaml | ||
import logging | ||
|
||
import pytest | ||
|
||
import bench | ||
from bench.common import Common as bcom | ||
|
||
p_logger = logging.getLogger("PYBENCH-PLUGGIN") | ||
# p_logger.setLevel(logging.INFO) | ||
|
||
def pytest_addoption(parser): | ||
group = parser.getgroup("pybench") | ||
group.addoption( | ||
"--configfile", | ||
action="store", | ||
dest="configfile", | ||
default=None, | ||
help="Set location of pybench config file", | ||
) | ||
|
||
@pytest.fixture(scope="session") | ||
def parse_instruments(request): | ||
configfile = request.config.getoption("--configfile") | ||
|
||
bc = bcom() | ||
if not configfile: | ||
bc._find_config() | ||
configfile = bc._config_file | ||
|
||
if not os.path.isfile(configfile): | ||
raise Exception(f"pybench config file not found at {configfile}") | ||
|
||
# Check status of each instrument | ||
with open(configfile, "r") as f: | ||
config = yaml.load(f, Loader=yaml.FullLoader) | ||
|
||
valid_instruments = {} | ||
|
||
for device in config: | ||
item = config[device] | ||
if "type" not in item.keys(): | ||
print(f"{item} has no field type. Skipping ...") | ||
continue | ||
if "address" not in item.keys(): | ||
print(f"{item} has no field address. Skipping ...") | ||
continue | ||
p_logger.info(f'Trying {item["type"]} at {item["address"]}') | ||
try: | ||
dev = eval(f'''bench.all.{item["type"]}(address='{item["address"]}')''') | ||
dev.connect() | ||
valid_instruments[item["type"]] = item["address"] | ||
dev.disconnect() | ||
except Exception as ex: | ||
p_logger.info(f"Failed to connect to {device}") | ||
p_logger.info(ex) | ||
continue | ||
|
||
return valid_instruments | ||
|
||
def _instrument_addresses(request, parse_instruments): | ||
print(dir(request)) | ||
marker = request.node.get_closest_marker("instruments_required") | ||
if hasattr(marker, "args") and len(marker.args): | ||
requested = marker.args[0] | ||
if not isinstance(requested, list): | ||
requested = [requested] | ||
applicable = [] | ||
# Check for requested instruments and filter out unnecessary ones | ||
for requested_instrument in requested: | ||
p_logger.info(f"Looking for {requested_instrument}") | ||
if requested_instrument in parse_instruments.keys(): | ||
applicable.append({requested_instrument: parse_instruments[requested_instrument]}) | ||
else: | ||
pytest.skip(f"Required Instrument not found ({requested_instrument}). Skipping...") | ||
return applicable | ||
return None | ||
|
||
@pytest.fixture(scope="function") | ||
def instrument_addresses(request, parse_instruments) -> dict: | ||
return _instrument_addresses(request, parse_instruments) | ||
|
||
@pytest.fixture(scope="session") | ||
def instrument_addresses_session(request, parse_instruments) -> dict: | ||
print("instrument_addresses_session") | ||
print(request.param) | ||
return _instrument_addresses(request, parse_instruments) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters