# How UVM knows "my\_test" class? why/where do we need to import "my\_pkg::\*" that holds the class "my\_test"?

**URL:** <https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545>\
**Category:** UVM\
**Tags:** import, uvm-package, UVM, factory, run\_test\
**Created:** [June 19, 2022, 1:45pm UTC](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545 "2022-06-19T13:45:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![uriyam](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/b77776/32.png) [@uriyam](https://verificationacademy.com/forums/u/uriyam)\
**Post date:** [June 19, 2022, 1:45pm UTC](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/1 "2022-06-19T13:45:31Z")

</div>

I’m looking at the “uvm hello world” example at [UVM Hello World - EDA Playground](https://www.edaplayground.com/x/296) ,  
and I’m wondering why do we need line 15 “import my\_testbench\_pkg::\*;”?

```verilog

/ *******************************************
This is a basic UVM "Hello World" testbench.

Explanation of this testbench on YouTube:
https://www.youtube.com/watch?v=Qn6SvG-Kya0
******************************************* /

`include "uvm_macros.svh"
`include "my_testbench_pkg.svh"

// The top module that contains the DUT and interface.
// This module starts the test.
module top;
  import uvm_pkg::*;
  import my_testbench_pkg::*;
  
  // Instantiate the interface
  dut_if dut_if1();
  
  // Instantiate the DUT and connect it to the interface
  dut dut1(.dif(dut_if1));
  
  // Clock generator
  initial begin
    dut_if1.clock = 0;
    forever #5 dut_if1.clock = ~dut_if1.clock;
  end
  
  initial begin
    // Place the interface into the UVM configuration database
    uvm_config_db#(virtual dut_if)::set(null, "*", "dut_vif", dut_if1);
    // Start the test
    run_test("my_test");
  end
  
  // Dump waves
  initial begin
    $dumpfile("dump.vcd");
    $dumpvars(0, top);
  end
  
endmodule

```

I understand that the compiler looks inside the imported pkg only if it doesn’t find the wanted name in the local scope.  
so I understand that we need to import “uvm\_pkg::\*” in order to use “run\_test()”,  
but nothing in “testbench.sv” is using any class within “my\_testbench\_pkg.svh”,  
so why do we need to import it?

what is the actual flow in uvm that makes it knows “my\_test” class?  
I understand it’s related to a factory and the line “`uvm\_component\_utils(my\_test)”,  
but why isn’t compilation enough?  
why do I have to use “import my\_testbench\_pkg::\*;”?  
why the test fails when I delete this import?  
(UVM\_FATAL @ 0: reporter [INVTST] Requested test from call to run\_test(my\_test) not found.)

---

<div class="post-metadata">

**Author:** ![chr\_sue](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chr_sue/32/1199_2.png) [@chr\_sue](https://verificationacademy.com/forums/u/chr_sue)\
**Post date:** [June 19, 2022, 2:16pm UTC](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/2 "2022-06-19T14:16:40Z")

</div>

_In reply to [uriyam](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545):_

You have to import my\_testbench\_pkg because it contains your testbench topology including the definition of the class “my\_test”.

---

<div class="post-metadata">

**Author:** ![uriyam](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/b77776/32.png) [@uriyam](https://verificationacademy.com/forums/u/uriyam)\
**Post date:** [June 19, 2022, 4:14pm UTC](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/3 "2022-06-19T16:14:55Z")

</div>

_In reply to [chr\_sue](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/2):_

so i’m including it, it compiles…  
why should I also import it in “testbench.sv” if no one is using it there?

I imagine that it needs to be imported inside the uvm\_pkg, inside run\_test(), maybe even in the factory, where uvm instantiate “my\_test” class. but obviously we can’t inject an import into the uvm files. so what is the mechanism?

---

<div class="post-metadata">

**Author:** ![chr\_sue](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chr_sue/32/1199_2.png) [@chr\_sue](https://verificationacademy.com/forums/u/chr_sue)\
**Post date:** [June 19, 2022, 4:40pm UTC](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/4 "2022-06-19T16:40:09Z")

</div>

_In reply to [uriyam](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/3):_

Of course you can include this file. In your simple environment it does not make any difference.  
Packaging files belonging together like agent data in a package allows you to import these data in any place you need it like a different project.

---

<div class="post-metadata">

**Author:** ![uriyam](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/b77776/32.png) [@uriyam](https://verificationacademy.com/forums/u/uriyam)\
**Post date:** [June 19, 2022, 4:50pm UTC](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/5 "2022-06-19T16:50:11Z")

</div>

_In reply to [chr\_sue](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/4):_

I don’t understand how your answer relates to my questions…?  
the pkg is already included:  
`include “my\_testbench\_pkg.svh”  
isn’t this enough? why should I also import it inside the testbench module if no one is using it there?

(I have a thought maybe it relates to some compiler optimization? maybe without the import the compiler thinks no one is using those classes so he through them away…?)

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 19, 2022, 6:06pm UTC](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/6 "2022-06-19T18:06:52Z")

</div>

_In reply to [uriyam](https://verificationacademy.com/forums/t/how-uvm-knows-my-test-class-why-where-do-we-need-to-import-my-pkg-that-holds-the-class-my-test/40545/5):_

Yes, that is correct, the compiler needs to know that someone is using the package.

Many compilation scripts compile much more than is needed for a particular simulation. Some tools require the user specify a top-level module, and the elaboration process figures out just the relevant units needed from that point downwards.

The UVM factory relies on initializing a static member of specialized parameterized class at time 0. That initialization calls a function that “registers” the class by adding an element to an array inside the UVM package. (See [this article](https://blogs.sw.siemens.com/verificationhorizons/2011/02/13/parameterized-classes-static-members-and-the-factory-macros/) for more about that)

But that initialization code only gets executed if it’s determined that the package is needed for the simulation. There are a number of ways that can happen, which may involve tool specific options:

- That package gets imported directly or indirectly into the design
- You tell the compiler the package is a top-level unit.
- You tell the compiler you want everything compiled to be part of the design.
