FileNotFoundError:[Errno 2]没有这样的文件或目录:'test_user1_user_id'

时间:2017-09-21 02:42:30

标签: python django

我收到了错误FileNotFoundError: [Errno 2] No such file or directory: 'test_user1_user_id'。我在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')

我运行python manage.py test& ./manage.py test app.tests,但两者都显示相同的错误。我肯定在user_id columnmodels.py,所以我真的不明白为什么会发生这种错误。我怎样才能解决这个问题?  我该怎么写呢?

这是追溯。

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'

1 个答案:

答案 0 :(得分:1)

问题与您的__init__方法有关:

def __init__(self, sheet_path):
    self.book = xlrd.open_workbook(sheet_path)
    self.sheet = self.book.sheet_by_index(1)

您收到此错误是因为初始化sheet_path时未提供CompanytransactionModelTests。我们可以从unittest.TextCase source看到modelName是第一个&amp;初始化TestCase时只有参数。我敢打赌,在类似test_user1_user_id代码的类上,你有一个名为__init__的方法。

setUp中设置测试数据是您正在做的事情的正确方法。如果你摆脱__init__代码,看起来应该可以正常工作。