Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7158141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:00:43+00:00 2026-05-28T13:00:43+00:00

I have written a function which basically hides the complication of using the pySnmp

  • 0

I have written a function which basically hides the complication of using the pySnmp library to send an SNMP message and it looks like:

snmpWalk(host,oid):
    from pysnmp.entity.rfc3413.oneliner import cmdgen
    cg = cmdgen.CommandGenerator()
    comm_data = cmdgen.CommunityData('my-manager','public')
    transport = cmdgen.UdpTransportTarget((host,161))
    variables = oid
    return cg.nextCmd(comm_data,transport,variables)

I am new to unit testing, and I was trying to write a unit test for this code as a doctest using minimock and this is what I came up with:

#Setup the mocks
>>> from minimock import mock, Mock
>>> cmdgen.CommandGenerator = Mock('cmdgen.CommandGenerator')
>>> cmdgen.CommunityData = Mock('cmdgen.CommunityData')
>>> cmdgen.UdpTransportTarget = Mock('cmdgen.UdpTransportTarget')
>>> cgMock = Mock('cgMock')
>>> cmdgen.CommandGenerator.mock_returns = cgMock
>>> cmdgen.CommunityData.mock_returns = 1
>>> cmdgen.UdpTransportTarget.mock_returns = 2
>>> cgMock.nextCmd.mock_returns = (1,2,3,4)
#run the test
>>> PrinterMonitor.SnmpWalk('192.0.0.1','1.1.1.1.1.1.1') 
Called cmdgen.CommandGenerator()
Called cmdgen.CommunityData('my-manager', 'public')
Called cmdgen.UdpTransportTarget(('192.0.0.1', 161))
Called cgMock.nextCmd(1, 2, (1, 1, 1, 1, 1, 1, 1))
(1, 2, 3, 4)

So I run this test and it works out fine, but my question is, do I really need to go to all this effort in mocking every aspect of the 3rd party library? In my mind the minimum I would need to mock is the final line cg.nextCmd(comm_data,transport,variables) because I don’t actually want to start firing off messages to printers in my unit tests, but the other calls to the library code should give me consistent results, depending on what I pass to them. I can make sure that my code interacts appropriately with the library (i.e. make sure that it passes the host argument to the transport variable correctly, by asserting this on the object passed to the mocked cg.nextCmd(...) call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:00:44+00:00Added an answer on May 28, 2026 at 1:00 pm

    Do you need to go through all that effort? No.

    To test code that uses your snmpWalk() function, you would be better to mock out the class that contains the function. I don’t know the Python terms, but in Java terms, I would put snmpWalk() in an interface, then have a pySnmp implementation and any number of mock/test implementations. The code calling snmpWalk() shouldn’t care about any internal details, it just cares about the return value, so that’s the level you want to mock at.

    To test the the pySnmp implementation class itself, you could mock out the library calls, but that’s a lot of work and is risky, given your mocks have to emulate the library very closely. I would just stick with the real calls. If that means firing off real printer requests, don’t run that test very often — or see if there’s a way to configure the library to print to virtual/fake printers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written my own function, which in C would be declared like this,
There's a function that I have written which basically does encryption for me 4
I have written a simple jQuery.ajax function which loads a user control from the
I have a function written in C# which has one parameter type as Dictionary<string
I have written a function for a multilevel wordpress menu, but I'd like it
I have written a function to print database table to an array like this
I have written a function which fires when a particular menu item is clicked.
I have written a function which takes a whole bunch of files, zips them
I have written a function that gets a given number of random records from
I have written a function that positions a tooltip just above a textbox. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.