Let's say you have a function to retrieve data from the database. Since the data changes frequently and the output for function like "get the nearest log of the system log" will have different output each time you call it. How do you properly write a Unit Test for this function? Is there any python library, framework to help you do it?
Asked
Active
Viewed 89 times
-1
-
1Typically you would create a separate data source (also known as "mocking") with known data, and then test that the function returns the correct result for that data. – John Gordon May 29 '18 at 04:09
-
1`unittest.mock` in the standard library – Reblochon Masque May 29 '18 at 04:11
-
@JohnGordon I can mark Accepted answer if you answer this question. Thank you. – Le D. Thang May 29 '18 at 04:38
1 Answers
0
You generally want to mock out pieces of your software that your the object under test depends on. Python3 provides unittest.mock which has all sorts of helpers for mocking out dependencies. However sometimes the simplest solution is to just create your own mock that provides consistent data that you can test against.

rsiemens
- 615
- 6
- 15