我正在使用Django和Python 3.7。我想创建并运行一些单元测试。根据Django文档,我创建了此文件...
ls ./mainpage/tests/test_models.py
但是当我使用下面的命令运行测试时,它说执行了零次测试...
(venv) localhost:mainpage_project davea$ cd /Users/davea/Documents/workspace/mainpage_project; source ./venv/bin/activate; python manage.py test
Creating test database for alias 'default'...
/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py:1421: RuntimeWarning: DateTimeField Article.main_page_first_appeared_date received a naive datetime (2019-01-29 22:43:53.575128) while time zone support is active.
RuntimeWarning)
System check identified no issues (0 silenced).
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
以下是我文件的内容。我想念什么?我应该在哪里放置测试,或者应该如何命名测试以便执行?
from django.test import TestCase
from mainpage.models import ArticleStat, Article
class TestModels(unittest.TestCase):
# 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.")
答案 0 :(得分:0)
我认为您想在一个目录中管理测试。这样。
.
├── app1
├── app2
├── ...
├── tests
│ ├── __init__.py
│ └── test_1.py
└── manage.py
您必须导入测试类。
# tests/__init__.py
from .test_1 import YourTestClass
然后,您可以通过python manage.py test tests
进行测试。如果只想运行test_1
模块,则命令为python manage.py test tests.test_1