如何在PyCharm中运行Django单元测试?

时间:2019-01-30 23:57:50

标签: python django python-3.x unit-testing pycharm

我在PyCharm 2018.2.4中使用的是Python 3.7,Django。我已将项目加载到PyCharm中,并打开了文件./mainpage/tests.py,这是

from django.test import TestCase
from mainpage.models import ArticleStat, Article
import unittest


class TestModels(unittest.TestCase):

    fixtures = ['/mainpage/fixtures/test_data.yaml',]

    # Test saving an article stat that hasn't previously
    # existed
    def test_add_articlestat(self):
        id = 1
        article = Article.objects.filter(id=id)
        self.assertTrue(article, "A pre-condition of this test is that an article exist with id=" + str(id))
        articlestat = ArticleStat(article=article,elapsed_time_in_seconds=250,votes=25,comments=15)
        articlestat.save()
        article_stat = ArticleStat.objects.get(article=article)
        self.assertTrue(article_stat, "Failed to svae article stat properly.")

我想在PyCharm中运行此测试文件。我转到“运行”菜单,然后选择“运行Tests.models的单元测试”。但是我得到这个错误

Error running 'Unittests for tests.TestModels': Can't find file where tests.TestModels declared. Make sure it is in project roo

要在IDE中运行测试,我需要在PyCharm中做什么?

0 个答案:

没有答案