How to convert csv into mock when csv file is specified as argument in unit test of Django command
How to convert csv into mock when csv file is specified as argument in unit test of Django command
There is a django command that gives the path of the csv file as an argument as follows.
class Command(NoticeCommand):
def add_arguments(self, parser):
parser.add_argument(
'--file', dest="file", type=str, required=True
)
def handle(self, *args, **options):
with open(options['file'], 'r') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
...
I am thinking to make this csv file mock when doing unit test.
However, I do not know which part and how to make mock.
Also, the argument is required = True
. How can I call UnitTest when csv is mocked?
required = True
from mock import patch
from django.core.management import call_command
class ImportCsvTest(TestCase):
@patch("common.management.commands.import_csv.????")
def test_import_csv(self):
call_command("import_lyric_artists", file=?????)
1 Answer
1
For more examples look to python documentation.
@patch('package.module.attribute', your_value)
def test():
from package.module import your_value
assert attribute is your_value
test()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.