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

  • SEARCH
  • Home
  • 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 8930267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:51:15+00:00 2026-06-15T08:51:15+00:00

I have an instance method that I want to test using mock. I am

  • 0

I have an instance method that I want to test using mock. I am trying to substitute one of the method in this instance class with another method that takes arguments.

class to be test:

class ClassToTest():
    def compute(self):
        result = self._get_xml()
        return result

    def _get_xml(self):
        #get xml here
        return xml    

The test file:

from mock import patch
class ClassTest():

    @patch('classToTest._get_xml', _get_fake_xml)
    def computetest(self):
        test = ClassToTest()
        toassert = test.compute()

        #assert whatever
        #self.assert(...)

    def _get_fake_xml(self, objects, filename):
        py_file = os.path.abspath(__file__)
        py_dir = os.path.dirname(py_file)
        xml_file = os.path.join(py_dir, filename)
        xml_tree = objectify.parse(xml_file)
        return xml_tree.getroot()
        return xml 

Now how can I add the arguments for _get_fake_xml method in the patch

I have tried :

@patch('classToTest._get_xml', _get_fake_xml(mock.Mock(), 'filenam.xml'))

But that didn’t work. Then I tried to make filename as a global variable but I get the following error:

self.filename does not exist in ClassToTest instance.

Basically I want to be able to reuse _get_fake_xml with any other filename.

Any suggestion?

  • 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-06-15T08:51:16+00:00Added an answer on June 15, 2026 at 8:51 am

    So, After some thoughts, I decided to use mock .return_value.

    @patch('classToTest._get_xml')
        def computetest(self, get_xml):
            get_xml.return_value = _get_fake_xml('filenam.xml')
            test = ClassToTest()
            toassert = test.compute()
    

    I wonder thought if there is a way to add the arguments in the patch decorator.

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

Sidebar

Related Questions

I have a class with an instance method that runs RSpec using the %x[]
I have an instance method that I'd like to invoke directly using the callback
I defined a custom instance method in the String class that I want to
I have an IBAction instance method that is connected to a slider, and displays
I have a method that calls a service to retrieve an instance of an
If I have for example a class with instance method and variables class Foo
I'm trying to load test a web service. I've got a simple method that
I have a test where I want to ensure separate operations within one page
I want to write unit test for a class that contains linq to sql
I have an app that navigates thru different views using the navigation controller. This

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.