def _initialise_testbench(root_name): """Initialize testbench. This function is called after the simulator has elaborated all entities and is ready to run the test. The test must be defined by the environment variables :envvar:`MODULE` and :envvar:`TESTCASE`. The environment variable :envvar:`COCOTB_HOOKS`, if present, contains a comma-separated list of modules to be executed before the first test. """ _rlock.acquire() memcheck_port = os.getenv('MEMCHECK') if memcheck_port is not None: mem_debug(int(memcheck_port)) log.info("Running tests with cocotb v%s from %s" % (__version__, os.path.dirname(__file__))) # Create the base handle type process_plusargs() # Seed the Python random number generator to make this repeatable global RANDOM_SEED RANDOM_SEED = os.getenv('RANDOM_SEED') if RANDOM_SEED is None: if 'ntb_random_seed' in plusargs: RANDOM_SEED = eval(plusargs['ntb_random_seed']) elif 'seed' in plusargs: RANDOM_SEED = eval(plusargs['seed']) else: RANDOM_SEED = int(time.time()) log.info("Seeding Python random module with %d" % (RANDOM_SEED)) else: RANDOM_SEED = int(RANDOM_SEED) log.info("Seeding Python random module with supplied seed %d" % (RANDOM_SEED)) random.seed(RANDOM_SEED) # Setup DUT object from cocotb import simulator handle = simulator.get_root_handle(root_name) if not handle: raise RuntimeError("Can not find root handle ({})".format(root_name)) dut = cocotb.handle.SimHandle(handle) # start Regression Manager global regression_manager regression_manager = RegressionManager.from_discovery(dut) regression_manager.execute() _rlock.release() return True
def _initialise_testbench_(argv_): # The body of this function is split in two because no coverage is collected on # the function that starts the coverage. By splitting it in two we get coverage # on most of the function. global argc, argv argv = argv_ argc = len(argv) root_name = os.getenv("TOPLEVEL") if root_name is not None: if root_name == "": root_name = None elif '.' in root_name: # Skip any library component of the toplevel root_name = root_name.split(".", 1)[1] # sys.path normally includes "" (the current directory), but does not appear to when python is embedded. # Add it back because users expect to be able to import files in their test directory. # TODO: move this to gpi_embed.cpp sys.path.insert(0, "") _setup_logging() # From https://www.python.org/dev/peps/pep-0565/#recommended-filter-settings-for-test-runners # If the user doesn't want to see these, they can always change the global # warning settings in their test module. if not sys.warnoptions: warnings.simplefilter("default") from cocotb import simulator global SIM_NAME, SIM_VERSION SIM_NAME = simulator.get_simulator_product().strip() SIM_VERSION = simulator.get_simulator_version().strip() cocotb.log.info("Running on {} version {}".format(SIM_NAME, SIM_VERSION)) memcheck_port = os.getenv('MEMCHECK') if memcheck_port is not None: mem_debug(int(memcheck_port)) log.info("Running tests with cocotb v%s from %s" % (__version__, os.path.dirname(__file__))) # Create the base handle type process_plusargs() global scheduler scheduler = Scheduler() # Seed the Python random number generator to make this repeatable global RANDOM_SEED RANDOM_SEED = os.getenv('RANDOM_SEED') if RANDOM_SEED is None: if 'ntb_random_seed' in plusargs: RANDOM_SEED = eval(plusargs['ntb_random_seed']) elif 'seed' in plusargs: RANDOM_SEED = eval(plusargs['seed']) else: RANDOM_SEED = int(time.time()) log.info("Seeding Python random module with %d" % (RANDOM_SEED)) else: RANDOM_SEED = int(RANDOM_SEED) log.info("Seeding Python random module with supplied seed %d" % (RANDOM_SEED)) random.seed(RANDOM_SEED) # Setup DUT object from cocotb import simulator handle = simulator.get_root_handle(root_name) if not handle: raise RuntimeError("Can not find root handle ({})".format(root_name)) global top top = cocotb.handle.SimHandle(handle) try: import pytest except ImportError: log.warning("Pytest not found, assertion rewriting will not occur") else: try: # Install the assertion rewriting hook, which must be done before we # import the test modules. from _pytest.config import Config from _pytest.assertion import install_importhook pytest_conf = Config.fromdictargs([], {}) install_importhook(pytest_conf) except Exception: log.exception( "Configuring the assertion rewrite hook using pytest {} failed. " "Please file a bug report!".format(pytest.__version__)) # start Regression Manager global regression_manager regression_manager = RegressionManager.from_discovery(top) regression_manager.execute() return True
def _initialise_testbench(argv_): """Initialize testbench. This function is called after the simulator has elaborated all entities and is ready to run the test. The test must be defined by the environment variables :envvar:`MODULE` and :envvar:`TESTCASE`. The environment variable :envvar:`COCOTB_HOOKS`, if present, contains a comma-separated list of modules to be executed before the first test. """ _rlock.acquire() if "COCOTB_LIBRARY_COVERAGE" in os.environ: import coverage global _library_coverage _library_coverage = coverage.coverage( data_file=".coverage.cocotb", branch=True, include=["{}/*".format(os.path.dirname(__file__))]) _library_coverage.start() global argc, argv argv = argv_ argc = len(argv) root_name = os.getenv("TOPLEVEL") if root_name is not None: if root_name == "": root_name = None elif '.' in root_name: # Skip any library component of the toplevel root_name = root_name.split(".", 1)[1] # sys.path normally includes "" (the current directory), but does not appear to when python is embedded. # Add it back because users expect to be able to import files in their test directory. # TODO: move this to gpi_embed.cpp sys.path.insert(0, "") _setup_logging() # From https://www.python.org/dev/peps/pep-0565/#recommended-filter-settings-for-test-runners # If the user doesn't want to see these, they can always change the global # warning settings in their test module. if not sys.warnoptions: warnings.simplefilter("default") from cocotb import simulator global SIM_NAME, SIM_VERSION SIM_NAME = simulator.get_simulator_product().strip() SIM_VERSION = simulator.get_simulator_version().strip() cocotb.log.info("Running on {} version {}".format(SIM_NAME, SIM_VERSION)) memcheck_port = os.getenv('MEMCHECK') if memcheck_port is not None: mem_debug(int(memcheck_port)) log.info("Running tests with cocotb v%s from %s" % (__version__, os.path.dirname(__file__))) # Create the base handle type process_plusargs() global scheduler scheduler = Scheduler() # Seed the Python random number generator to make this repeatable global RANDOM_SEED RANDOM_SEED = os.getenv('RANDOM_SEED') if RANDOM_SEED is None: if 'ntb_random_seed' in plusargs: RANDOM_SEED = eval(plusargs['ntb_random_seed']) elif 'seed' in plusargs: RANDOM_SEED = eval(plusargs['seed']) else: RANDOM_SEED = int(time.time()) log.info("Seeding Python random module with %d" % (RANDOM_SEED)) else: RANDOM_SEED = int(RANDOM_SEED) log.info("Seeding Python random module with supplied seed %d" % (RANDOM_SEED)) random.seed(RANDOM_SEED) # Setup DUT object from cocotb import simulator handle = simulator.get_root_handle(root_name) if not handle: raise RuntimeError("Can not find root handle ({})".format(root_name)) dut = cocotb.handle.SimHandle(handle) # start Regression Manager global regression_manager regression_manager = RegressionManager.from_discovery(dut) regression_manager.execute() _rlock.release() return True
def initialise(self): self.start_time = time.time() self.test_results = [] self.ntests = 0 self.count = 1 self.skipped = 0 self.failures = 0 # Setup XUnit ################### results_filename = os.getenv('COCOTB_RESULTS_FILE', "results.xml") suite_name = os.getenv('RESULT_TESTSUITE', "all") package_name = os.getenv('RESULT_TESTPACKAGE', "all") self.xunit = XUnitReporter(filename=results_filename) self.xunit.add_testsuite(name=suite_name, tests=repr(self.ntests), package=package_name) if (self._seed is not None): self.xunit.add_property(name="random_seed", value=("%d" % self._seed)) # Setup Coverage #################### if coverage is not None: self.log.info("Enabling coverage collection of Python code") self._cov = coverage.coverage(branch=True, omit=["*cocotb*"]) self._cov.start() # Setup DUT object ####################### handle = simulator.get_root_handle(self._root_name) self._dut = cocotb.handle.SimHandle(handle) if handle else None if self._dut is None: raise AttributeError("Can not find Root Handle (%s)" % self._root_name) # Test Discovery #################### have_tests = False for module_name in self._modules: try: self.log.debug("Python Path: " + ",".join(sys.path)) self.log.debug("PWD: " + os.getcwd()) module = _my_import(module_name) except Exception as E: self.log.critical("Failed to import module %s: %s", module_name, E) self.log.info("MODULE variable was \"%s\"", ".".join(self._modules)) self.log.info("Traceback: ") self.log.info(traceback.format_exc()) raise if self._functions: # Specific functions specified, don't auto-discover for test in self._functions.rsplit(','): try: _test = getattr(module, test) except AttributeError: self.log.error( "Requested test %s wasn't found in module %s", test, module_name) err = AttributeError("Test %s doesn't exist in %s" % (test, module_name)) raise err from None # discard nested traceback if not hasattr(_test, "im_test"): self.log.error( "Requested %s from module %s isn't a cocotb.test decorated coroutine", test, module_name) raise ImportError("Failed to find requested test %s" % test) self._init_test(_test) # only look in first module for all functions and don't complain if all functions are not found break # auto-discover for thing in vars(module).values(): if hasattr(thing, "im_test"): self._init_test(thing) have_tests = True if not have_tests: self.log.warning("No tests were discovered") self._queue.sort(key=lambda test: (test.stage, test._id)) for valid_tests in self._queue: self.log.info("Found test %s.%s" % (valid_tests.module, valid_tests.funcname)) # Process Hooks ################### for module_name in self._hooks: self.log.info("Loading hook from module '" + module_name + "'") module = _my_import(module_name) for thing in vars(module).values(): if hasattr(thing, "im_hook"): try: test = thing(self._dut) except Exception: self.log.warning("Failed to initialize hook %s" % thing.name, exc_info=True) else: cocotb.scheduler.add(test)