Reputation: 23
I have a long running python method, this function will go inside folders and read all the documents and extract text. Number of output will not going to be a constant as it can change. This process might take from several hours to days. How to write a unit test for such a function, how to check whether its pass or fail as the number of output might change and it takes many hours
Upvotes: 0
Views: 1422
Reputation: 22942
This kind of problem can be solve by using test resources. This is a classic problem you can have when your need to test a function on file data.
In your tests
directory (you have one, isn’t it?), you can create a resources
package.
All you need is to put your test data here. Of course, choose a minimal tree structure: the only necessary files and directories required for your test.
To access your resources
directory I recommend using a RESOURCES_DIR
variable define as follow:
In your __init__.py
file, you can write:
import os
RESOURCES_DIR = os.path.dirname(__file__)
You can easily import this variable in your unit test and use it to access your test files.
Here is a more detailed example:
tests
+-- resources
| +-- __init__.py
| \-- test_func1
| +-- scenario1
| | +-- <data for scenario1>
| \-- scenario2
| \-- <data for scenario2>
|
+-- test_func1.py
Your test_func1.py
can look like this:
import unittest
import os
from tests.resources import RESOURCES_DIR
class TestFunc1(unittest.TestCase):
def test_func1_scenario1(self):
data_dir = os.path.join(RESOURCES_DIR, 'test_func1/scenario1')
...
def test_func1_scenario2(self):
data_dir = os.path.join(RESOURCES_DIR, 'test_func1/scenario2')
...
Upvotes: 2