0

I'm new to testing with Python and am not sure if this is even possible.

I have a relatively long method that accepts an input, does some processing then sends the data to an API.

I would like to write a test that will send the inputted data to the test, run the processing on the data but NOT send it to the API. So basically run for a certain amount within the method but not to the end.

Unfortunately I'm not even sure where to start so I can't really provide relevant sample code - It would just be a standard unit test that runs a method with input and asserts the output.

1
  • 2
    In my opinion, you should extract the pieces of code you want to test into their own functions. That way you can test the parts you're interested in. Commented Oct 31, 2017 at 14:19

1 Answer 1

4

You are taking the wrong approach. What you want to do is execute your test isolated from your external API function calls. Just mock your API calls. That means, run your test with the API calls replaced with mock methods. You don't need to change code under test, you can use a patch decorator to replace the API calls with mock objects. Please see the unittest.mock documentation and examples here

unittest.mock is very powerful, and can look a bit daunting or at least a bit puzzling at the beginning. Take your time to understand the kinds of things you can do with mocks in the documentation. A very simple example here, of one of the possibilites (in some test code):

@patch('myproject.db.api.os.path.exists')
def test_init_db(self, mock_exists):
     ...
     # my mock function call will always returns False
     mock_exists.return_value = False

     # now calls to myproject.db.api.os.path.exists
     # in the code under test act just like the db file does not exist
     ...

So you probably can bypass your external API calls (all of them or some of them) with ease. And you don't have to specify API results if you don't want to. Mocks exhibit "plastic" behaviour.

If you create a mock and call an arbitrary mock method you haven't even defined (think the API methods you want to isolate) it will run ok and simply return another mock object. That is, it will do nothing, but its client code will still run as if it did. So you can run your tests actually disabling the parts you want.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem isn't that the thing they want to test is calling unwanted APIs. The problem is that the thing they're interested in is mixed in with other code they don't want to execute.
I think this is the approach that OP was looking for but without knowing about mocking.
Just edited for a better explanation towards OP question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.