0

How to write unit test for below:

def processDbOutput(dbConfigObject, tuple): 
    dbConn = DBConnectionProvider(dbConfigObject.dbServer) 
    res=dbConn.excecute(......)

Want to write unit test for processDbOutput method accepting parameters. And also mock the dbConn inside the method.

(update) The processDbOutput method is target method for multiprocessing. So it is good practice to create and close db connection within the same method. (Correct me if I am wrong).

Thanks in advance.

4
  • What do you want to test? If you mock DBConnectionProvider (and thus dbConn), you can test the parameters execute is called with. Other than that, it is diffcult to guess what you want to do. Best you show us your test code (even if it is not working) to better describe your problem. Commented Jun 6, 2020 at 13:45
  • Hi Bremen, can you provide me test code for above scenario. I am not sure how do i write test case to achieve this. Commented Jun 6, 2020 at 13:54
  • I want to test the processDbOutput method which accepting database config obaject & tuple to process. I want to: 1. db config object which is parameter (need to mock) 2. using the dbconfig object I create db connection (need to mock) Commented Jun 6, 2020 at 14:01
  • I think you didn't understand the question. I understand that you want to mock several things (which you can do using standard mock.patch), but I want to know what do you want to test. E.g. can you write an example test with some assertions to show what you are going to test in processDbOutput? Commented Jun 6, 2020 at 14:05

2 Answers 2

1

Your processDbOutput has two responsibilities. One is create db connection, and another is execute something. It's recommended to split this function like below:

def connectDb(dbConfigObject):
    return DBConnectionProvider(dbConfigObject.dbServer)

def executeDb(dbConn, tuple):
    res=dbConn.execute(......)

Then, you can test executeDb with mock, dbConn.

If you are re-structuring your code for testing, your code will be more beautiful.

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

1 Comment

Thank you, I have updated question based on multiprocessing aspect.
0

I think that 정도유's answer can still apply. I still think the answer is dependency injection, but I would inject a function to initialize the connection:

def connectDb(dbConfigObject):
    return DBConnectionProvider(dbConfigObject.dbServer)

def executeDb(initializeDB, tuple):
    dbConn = initializeDB()
    res=dbConn.execute(......)

This enables your test to provide a dummy dbConn object for unit testing instead of patching. Then your production code can use DBConnectionProvider() directly:

def test_executeDb(self):
   m = MagicMock()
   # configure m.execute.return_value, etc
   def initialize_stub():
       return m

   executeDb(initialize_stub, m)
   # assertion

Comments

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.