You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
More testing: learning to mock a database dependency for testing
Item 2: What we did
Reviewed how we made one of our API endpoints more testable by injecting our dependency on the database library into the function
Created a more sustainable folder structure for our tests that matches the folder structure of our source code
This helps as the project gets larger it becomes more manageable to find the corresponding tests for different source code
Talked through making our route code more testable by abstracting or moving our code/logic for querying the database into a separate class/module
We then moved the database querying code into a separate "services" file which led us to update the route function we're focused on testing to leverage that new "service"
unittest.mock.AsyncMock is built into Python and allows us to mock asynchronous code for our unit tests
Used the following to help mock the dependency function and value we want to test @patch('<module.functionToMock>', new=AsyncMock(return_value=<the value you want>)
Item 3: Something we learned
unittest.mock.AsyncMock is built into Python and allows us to mock asynchronous code for our unit tests
@patch helps us replace the behavior and/or what's returned in a mocked function
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Catch recordings of these streams in the YouTube playlist
Links of the day:
Recap from last session:
Started work on implementing tests with pytest
Item 1: Goal for today's session
Item 2: What we did
@patch('<module.functionToMock>', new=AsyncMock(return_value=<the value you want>)
Item 3: Something we learned
unittest.mock.AsyncMock
is built into Python and allows us to mock asynchronous code for our unit tests@patch
helps us replace the behavior and/or what's returned in a mocked functionNext Session
Beta Was this translation helpful? Give feedback.
All reactions