Test::Mock::Net::SNMP - Perl extension for mocking Net::SNMP in your unit tests.


  use Test::Mock::Net::SNMP;
  my $mock_snmp = Test::Mock::Net::SNMP->new();


Test::Mock::Net::SNMP is a simple way to mock a Net::SNMP object and allows you to test your modules behaviour when retrieving SNMP data or sending SNMP traps.



my $mock_net_snmp = Test::Mock::Net::SNMP->new();

Generates the mock object required to mock Net::SNMP


$mock_net_snmp->set_varbindlist( [ {'' => 1, '' => 2, '' => 3, '' => 4}, {'' => 5, '' => 1, '' => 2, '' => 5} ] );

set_varbindlist is the main way of returning values in Net::SNMP the most important part of setting up the mock is setting this correctly takes an array reference of varbindlist hashes and returns true

This will also set up varbindnames, but you can overwrite this by calling set_varbindnames (see below). set_varbindlist will overwrite varbindnames so it's best to call this first.

To force a failed return for a request method or a call to var_bind_list assign a value of undef to the array element that represents that call.

i.e. if we have a blocking get_request that is performed after two set request, and we want that request to fail to make sure that our code is handling a the failure correctly, we could set it up like this:

$mock_net_snmp->set_varbindlist( [ { '' => 'Help Desk x911', '' => 'Building 1, First Floor' }, { '' => 'Help Desk x911', '' => 'Building 1, Second Floor' }, undef, { '' => 600 } ] );


$mock_net_snmp->set_varbindnames([[qw( 2.2.1 2.2.3 2.2.4 )]]);

varbindnames is a list of names for each oid, it should match the keys of the hash that the call to var_bind_list returns.

set_varbindnames takes an array reference of arrays of oids.


$mock_net_snmp->set_varbindtypes( [ { '' => OCTET_STRING, '' => OCTET_STRING, '' => OCTET_STRING }, { '' => OCTET_STRING, '' => OCTET_STRING, '' => OCTET_STRING } ] );

varbindtypes is a hash of types for each oid

set_varbindtypes takes an array reference of varbindtypes



calling this method will mean that all calls to Net::SNMP->session will fail.

To revert this you need to call reset_values (see below)


$mock_net_snmp->set_error('Error message');

This method allows you to override the error message that will be returned if an error occurs.



This lets you set the return value of an $snmp->error_status() call.



This lets you set the return value of an $snmp->error_index() call.


is($mock_net_snmp->get_option_val('session','-hostname'),q{},q{correct hostname passed to session});

is_deeply($mock_net_snmp->get_option_val('get_request','-varbindlist',0),[''],q{first call to get_request is for});

is($mock_net_snmp->get_option_val($method,$option,$position), $expected, qq{$option passed to $method in call $postition is $expected});

where: $method is the mocked method, $option is the option passed into the method, $position is the position in the call stack (the last call is returned if no position is given)

it returns the value for that option.

Net::SNMP lets you specify options in a style such as -varbindlist or Varbindlist. Test::Mock::Net::SNMP expects you to retrieve the option values using the same style as the option passed in. So if your method call uses Varbindlist then $option should equal Varbindlist.



returns the number of times that the requested method was called.



Sets all the values to their original state.



Test::Mock::Net::SNMP will only update the error string if it hasn't already been set. This means that sometimes it is useful to clear the error string



force a trap method to fail.


Some additional checks are performed to make sure that:

1.) The methods that expect a -varbindlist option are passed one 2.) The first two oids of an snmpv2_trap and inform_request are there 3.) those functions that should pass multiples of three do. 4.) checks that some arguments are within range.

It's important to not rely on these checks and instead check them through your unit tests, but you may find your tests dying if the input values are known to be incorrect.


Net::SNMP, Test::MockObject::Extends


Rob Halliday, <>


Copyright (C) 2013 by Rob Halliday

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.