首页 > 解决方案 > 在 VUnit 中测试断言失败

问题描述

如果他们不满意,我可能有一些功能会断言并失败。

如何使用 VUnit 测试此功能以确保这些功能确实在正确的条件下引发故障?


例如,假设我想检查这个函数:

function to_normalised_float(slv: std_logic_vector) return half_float is
variable temp_hf: half_float;
begin
    assert slv'high = 15 report "SLV provided is incorrect length for an FP16";
    -- ConstructFloat
    return normalise_float(temp_hf);
end function;

如果我传入一个值然后在我的测试台的输出上断言,我可以很容易地测试它是否返回预期值。

但是,我还希望能够使用 VUnit 测试,如果我传入 22 位 SLV,断言会抛出。

这显然是一个非常简化的例子,但它应该解释我的意思。

Assert.Throws(function)如果有帮助,C# 中的等价物将是。

标签: vhdlvunit

解决方案


检查断言的能力将随着您的模拟器中的 VHDL-2019 支持而提高,但由于您使用的是 VUnit,我建议使用 VUnit 模拟(http://vunit.github.io/logging/user_guide.html#mockinghttps:// github.com/VUnit/vunit/blob/f02c21452a505c527db575b10db94195ceb7ed2f/vunit/vhdl/logging/src/logger_pkg.vhd#L342),这是为了支持您的用例而提供的。

首先用assertVUnit替换你的check

check(slv'high = 15, "SLV provided is incorrect length for an FP16");

当该检查失败时,您将看到如下所示的错误消息:

0 ps - check - ERROR - SLV provided is incorrect length for an FP16

checklogger是管理此消息的 VUnit 。您可以按名称 ( get_logger("check")) 获取此记录器并对其进行模拟。模拟意味着所有输出消息(具有特定严重性级别)将被放置在一个队列中,而不是传递给标准输出。可以检查此队列中的消息以确定该功能是否按预期工作。这是一个稍微修改的示例测试台来展示原理

library vunit_lib;
context vunit_lib.vunit_context;

library ieee;
use ieee.std_logic_1164.all;

entity tb_example is
  generic (runner_cfg : string);
end entity;

architecture tb of tb_example is
begin
  main : process
    procedure dummy(slv : std_logic_vector) is
    begin
      check(slv'length = 16, "SLV provided is incorrect length for an FP16");
    end;

    constant logger : logger_t := get_logger("check");
  begin
    test_runner_setup(runner, runner_cfg);

    while test_suite loop
      if run("Test to see dummy fail") then
        dummy(x"17");
      elsif run("Test that dummy fails with the correct message") then
        mock(logger, error);
        dummy(x"17");
        check_log(logger, "SLV provided is incorrect length for an FP16", error);
        unmock(logger);
      elsif run("Test that dummy passes with 16 bit inputs") then
        mock(logger, error);
        dummy(x"1718");
        check_no_log;
        unmock(logger);
      end if;
    end loop;

    test_runner_cleanup(runner);
  end process;

end architecture;

第一个测试用例将失败(这是您的问题),但最后两个将通过

在此处输入图像描述

我还可以推荐使用check_equal以获得更多信息的输出。

check_equal(slv'length, 16, result("for input length"));

会给你以下错误输出:

0 fs - check - ERROR - Equality check failed for input length - Got 8. Expected 16.

推荐阅读