unit test并不是发现现有代码的bug,而是进行功能测试,即测试现有代码是否完成期望的功能。而代码的bug,更像是安全性测试,不属于unit test范畴。
unit test 要求:
必须毫秒级的运行。
能独立运行。
不能依赖外部输入。
我们回顾c++, python的unit test,我们会发现,所有unit test遵循以下结构:
assume(arrange):提供激励。
act:激励施加到dut上。
assert:检查dut的输出。
testfixture: 事先定义的common code,例如:
class SystemCFixture : public testing::Test {
protected:
void SetUp() override; // Build the top level and start simulation.
void TearDown() override; // Restart the simulator at the end.
virtual void BuildTestbenchTop() = 0; // Construct and bind modules
static void AdvanceSimulationTime(const sc_time& timeOffset = sc_max_time());
private:
static void RestartSim();
};
最后说一下mock,stub,fake的区别。
mock是代替外部接口。
stub产生预定义的输出。
fake是接近可以工作的实现。
比较好的介绍Googletest framework:
https://www.youtube.com/watch?v=x5_UxQ9wrH4&list=PL_dsdStdDXbo-zApdWB5XiF2aWpsqzV55&index=2
该链接给出用google test框架来测试TLM2 模型。
https://github.com/cphurley82/tlm2-gtestbench
synopsys有TLM creator工具帮助测试LT model,使用的是c++unittest框架。