NAB0815
NAB0815

Reputation: 471

Python unittest returns MagicMock object instead of return value

I have a python which has a class A. Class A object has a method that takes a list as input and sends a post request to a client endpoint to create new sources in the database and returns a tuple of successful and unsuccessful lists

class A():
  def create_sources(self):
     successful = []
     unsuccessful = []
     new_sources = self.get_new_sources() -> ['x','y']
     if len(new_sources):
         for source in new_sources:
             req = {
                     JSON object
                    }
   
      response = request.post("https://example.com", auth_token, req)
      if response.status_code == 200:
         successful.append(source)
      if response.status_code != 200:
         unsuccessful.append(source)

    print(f"Successful: {successful}; Unsuccessful: {unsuccessful}")
    tup = (successful, unsuccessful)
    return tup

This is my unit test:

@pytest.fixture
def a_config():
    a_config = Mock(spec=AConfig)
    a_config.base_url = Mock(return_value="https://example.com")
    a_config.auth_token = Mock(return_value="abcdefg")
    return a_config


@pytest.fixture
def a_cs(a_config):
    a_cs = A(a_config)
    return a_cs


class TestA(object): 
      @mock.patch('path.A.create_sources')
      def test_create_new_sources_success:
          A.get_new_sources = Mock(return_value=['test'])
          successful_unsuccessful = A.create_new_sources()
          print(successful_unsuccessful)
          assert len(successful_unsuccessful[0]) == 1
          assert len(successful_unsuccessful[1]) == 0
            

I wanted to see what successful_unsuccessful looks like and the test is returning a MagicMock object <MagicMock name='create_sources.create_new_sources()' id='5039795504'>

What I want to return is a tuple object. What am I doing wrong here?

Upvotes: 4

Views: 628

Answers (0)

Related Questions