mbits-mirafra / axi4_avip

MIT License
21 stars 20 forks source link

SVUnit - SVMock Framework #82

Open muneebullashariff opened 8 months ago

muneebullashariff commented 8 months ago

http://agilesoc.com/2018/07/11/svmock-version-0-1/ pdf file version SVMock Version 0.1 _ AgileSoC.pdf

http://agilesoc.com/2018/07/24/testing-uvm-drivers-without-the-sequencer/ pdf version Testing UVM Drivers (Without The Sequencer) _ AgileSoC.pdf

https://blog.verificationgentleman.com/2016/08/07/testing-uvm-drivers

https://blog.verificationgentleman.com/2016/08/15/testing-uvm-drivers-part-2 https://github.com/verification-gentleman-blog/testing_uvm_drivers_pt2

For more useful macros: https://github.com/tudortimi/vgm_svunit_utils/tree/master

http://agilesoc.com/2018/08/06/unit-testing-uvm-sequences/ Ready made mock for testing uvm-sequences under src/uvm-mock. Unit Testing UVM Sequences _ AgileSoC.pdf

http://agilesoc.com/2012/09/21/svunit-by-example-a-simple-uvm-model/ pdf version SVUnit by Example A Simple UVM Model AgileSoC.pdf

http://agilesoc.com/2012/03/09/uvm-express-step-2-svunit-with-covergroups-and-uvm-agents/ pdf version UVM Express Step 2 SVUnit with Covergroups and UVM Agents AgileSoC.pdf

http://agilesoc.com/2012/02/20/by-example-test-driven-development-of-verification-ip/

muneebullashariff commented 8 months ago

https://github.com/svunit/svmock

This section talks about as how to use the SVMock to mock the dependencies to test the uut in isolation. It also shows examples for driver and sequence testing using the pre-defined mocking templates. Furthermore, it talks about writing unit test for coverage groups too Lastly it shows the unit_test for interface and bfm

muneebullashariff commented 8 months ago

Went through the SVMock and we should use it for mocking any uvm dependent classes. There are predefined examples as how to verify the uvm_driver, sequence etc using the SVMock and it uses SVUnit as the base so it's a natural step to use SVMock.

muneebullashariff commented 8 months ago

Recording my learnings:

  1. We can even mock a file which has a parent

Image

  1. We need to write the below to mock a class. We can even try to write a script to automatically generate the below file for a given file. This will work if we have the class defined using the extern function/task so we can easily do this work.

Image

VFUNC means has void as the return FUNC means with return

muneebullashariff commented 8 months ago
  1. FUNC means the number of input arguments

  2. First argument is the name of the function Second argument is the return type, if it's non-void Followed by the input arguments. Task is same as void-function

Image

muneebullashariff commented 8 months ago
  1. bedrock.sv file is the UUT flintstones.sv is the file to mock Write a flintstone_mock.sv file using the macros Create a package file bedrock_pkg.sv and include bedrock and flintstone files Generate the bedrock_unit_test using the create_unit_test.pl and then add below lines

Add the below lines too.

Image

This step can be clubbed with step2 script to make it easy to mimic this type of dependencies.

Also, for frequent mocking dependencies, create the files like done inside

muneebullashariff commented 8 months ago
  1. When you create your project and use this svmock project then create a file and the below:

custom_svunit.f

//-parseinfo macro

+incdir+/hwetools/work_area/frontend/muneeb_new/svmock/src

/hwetools/work_area/frontend/muneeb_new/svmock/src/svmock_pkg.sv

runSVUnit -f custom_svunit.f -s questa

Can name it as only svunit.f also for consistency This will allow the runSVUnit to figure out the svmock libraries correctly.

We can even use the below environmental variable to make it easy:

setenv SVMOCK_INSTALL /hwetools/svmock/ Similarly to SVUNIT_INSTALL

muneebullashariff commented 8 months ago

Image

Thus, custom_svunit.f file becomes as below:

Image

muneebullashariff commented 8 months ago
  1. Not only we can override the functionality of the functions or tasks, but also can have multiple implementations mapped.

Image

As shown above, the same function dino has been mapped to 2 different functions, mr_slate and another_mr_slate. This feature allows us to have different implementations for the same function call.

Image

As shown above, while calling the function we need tell which function we are overriding and with new functions (as there are 2 newly mapped functions)

muneebullashariff commented 8 months ago

Image

muneebullashariff commented 8 months ago
# INFO:  [0][bedrock_ut]: rock_quarry_called_instead_pebbles::RUNNING
# ------ test [6] starting ------
# rock_quarry called
# EXPECT_CALL::with_args[0].miscompare bam_bam::barney: (exp:12 act:0)
# ERROR: [0][bedrock_ut]: fail_unless: mock_f.verify() (at /hwetools/work_area/frontend/muneeb_new/test_svmock/bedrock/./bedrock_unit_test.sv line:57)
# INFO:  [0][bedrock_ut]: rock_quarry_called_instead_pebbles::FAILED
muneebullashariff commented 8 months ago

Experiment 1: The overriding is done, but the old function pebbles is called

Image

As shown above, we are setting the return value for pebbles function as 99 however, we are overriding it with rock_quarry, hence the return value of rock_quarry will be effective, which is Zero

# INFO:  [0][bedrock_ut]: rock_quarry_called_with_pebbles_return_value::RUNNING
# ------ test [7] starting ------
# rock_quarry called
# EXPECT_CALL::with_args[0].miscompare bam_bam::barney: (exp:99 act:0)
# ERROR: [0][bedrock_ut]: fail_unless: mock_f.verify() (at /hwetools/work_area/frontend/muneeb_new/test_svmock/bedrock/./bedrock_unit_test.sv line:57)
# INFO:  [0][bedrock_ut]: rock_quarry_called_with_pebbles_return_value::FAILED
muneebullashariff commented 8 months ago

Experiment 2: Setting the return value for the overridden function - rock_quarry

Image

Even this won't work, because the overridden function is NOT-A-MOCK function, hence the return method won't work.

# INFO:  [0][bedrock_ut]: rock_quarry_called_with_pebbles_return_value::RUNNING
# ------ test [7] starting ------
# rock_quarry called
# EXPECT_CALL::with_args[0].miscompare bam_bam::barney: (exp:99 act:0)
# ERROR: [0][bedrock_ut]: fail_unless: mock_f.verify() (at /hwetools/work_area/frontend/muneeb_new/test_svmock/bedrock/./bedrock_unit_test.sv line:57)
# INFO:  [0][bedrock_ut]: rock_quarry_called_with_pebbles_return_value::FAILED
# INFO:  [0][bedrock_ut]: FAILED (3 of 7 tests passing)
muneebullashariff commented 8 months ago

Experiment 3: Only way is to write into the overridden function the return value

Image

# INFO:  [0][bedrock_ut]: rock_quarry_called_with_pebbles_return_value::RUNNING
# ------ test [7] starting ------
# rock_quarry called
# INFO:  [0][bedrock_ut]: rock_quarry_called_with_pebbles_return_value::PASSED
muneebullashariff commented 8 months ago

For interface-class mocking, refer