Reputation: 105
I got an error FileNotFoundError: [Errno 2] No such file or directory: 'test_user1_user_id'
. I wrote in tests.py
from datetime import datetime
from django.test import TestCase
from app.models import Companytransaction
import xlrd
# Create your tests here.
class CompanytransactionModelTests(TestCase):
def __init__(self, sheet_path):
self.book = xlrd.open_workbook(sheet_path)
self.sheet = self.book.sheet_by_index(1)
def setUp(self):
self.book = xlrd.open_workbook('./data/excel1.xlsx')
self.sheet = self.book.sheet_by_index(1)
num = 0
for row_index in range(2,4):
row = self.sheet.row_values(row_index)
user = Companytransaction(user_id=row[1], name=row[2], age=row[3])
user.save()
if num == 0:
self.user1 = Companytransaction.objects.create(user_id=row[1], name=row[2], age=row[3])
num += 1
elif num == 1:
self.user2 = Companytransaction.objects.create(user_id=row[1], name=row[2], age=row[3])
num += 1
else:
self.user3 = Companytransaction.objects.create(user_id=row[1], name=row[2], age=row[3])
def test_user1_company_id(self):
self.assertEqual(self.user1.user_id, '100')
def test_user1_corporation_id(self):
self.assertEqual(self.user1.name, 'Tom')
def test_user1_company_name(self):
self.assertEqual(self.user1.age, '29')
I run python manage.py test
& ./manage.py test app.tests
, but both of them shows the same error. I surely made user_id column
in models.py
, so I really cannot understand why this error happens. How can I fix this?
What should I write this?
Here's the traceback.
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/core/management/commands/test.py", line 62, in handle
failures = test_runner.run_tests(test_labels)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/test/runner.py", line 600, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "/Users/xxx/myenv/lib/python3.5/site-packages/django/test/runner.py", line 484, in build_suite
tests = self.test_loader.loadTestsFromName(label)
File "/Users/xxx/.pyenv/versions/3.5.0/lib/python3.5/unittest/loader.py", line 190, in loadTestsFromName
return self.loadTestsFromModule(obj)
File "/Users/xxx/.pyenv/versions/3.5.0/lib/python3.5/unittest/loader.py", line 123, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "/Users/xxx/.pyenv/versions/3.5.0/lib/python3.5/unittest/loader.py", line 92, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "/Users/xxx/.pyenv/versions/3.5.0/lib/python3.5/unittest/suite.py", line 24, in __init__
self.addTests(tests)
File "/Users/xxx/.pyenv/versions/3.5.0/lib/python3.5/unittest/suite.py", line 57, in addTests
for test in tests:
File "/Users/xxx/app/app/tests.py", line 12, in __init__
self.book = xlrd.open_workbook(sheet_path)
File "/Users/xxx/myenv/lib/python3.5/site-packages/xlrd/__init__.py", line 116, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'test_user1_user_id'
Upvotes: 2
Views: 2613
Reputation: 4354
It looks like the issue is with your __init__
method:
def __init__(self, sheet_path):
self.book = xlrd.open_workbook(sheet_path)
self.sheet = self.book.sheet_by_index(1)
You're getting this error because you aren't providing sheet_path
when CompanytransactionModelTests
is initialized. We can see from the unittest.TestCase source that modelName
is the first & only argument when initializing a TestCase
. I'd bet a penny that you've got a method called test_user1_user_id
on a class with similar __init__
code.
Setting up that test data in setUp
is the right way to do what you're doing. It looks like things should work properly if you get rid of that __init__
code.
Upvotes: 2