In unit test I create 3 objects (articles) at the beginning. After test complete I notice that there are 3 images in media_root
folder.
Question: How to delete that images which was created after test finished?
P.S. I tried to use next code but it delete media_root
folder.
def tearDown(self):
rmtree(settings.MEDIA_ROOT, ignore_errors=True)
NOTE: Problems in methods test_article_form_valid
and test_article_crud
.
tests.py:
class ArticleTestCase(TestCase):
def setUp(self):
self.image = open(os.path.join(BASE_DIR, 'static/images/tests/image.jpg'), "r")
def test_article_form_valid(self):
data = {
'head': 'TEXT',
}
files_data = {
'image': SimpleUploadedFile(
name=self.image.name,
content=self.image.read(),
content_type='image/jpeg'
)
}
form = ArticleForm(data=data, files=files_data)
self.assertTrue(form.is_valid()) <-- ERROR
def test_article_crud(self):
response = self.client.get(reverse("article:article_create"))
self.assertEquals(response.status_code, 200)
response = self.client.post(
reverse("article:article_create"),
data={
'head': 'TEST',
'image': self.image
},
follow=True,
format='multipart'
)
self.assertEqual(response.status_code, 200)
self.assertEqual(Article.objects.all().count(), 1) <-- ERROR
def test_article_view(self):
first_article = Article.objects.create(
pk=150,
head='First',
image=SimpleUploadedFile(
name=self.image.name,
content=self.image.read(),
content_type='image/jpeg'
)
)
second_article = Article.objects.create(
pk=160,
head='Second',
image=SimpleUploadedFile(
name=self.image.name,
content=self.image.read(),
content_type='image/jpeg'
)
)
third_article = Article.objects.create(
pk=170,
head='Third',
image=SimpleUploadedFile(
name=self.image.name,
content=self.image.read(),
content_type='image/jpeg'
)
)
[***]
ERROR:
FAIL: test_article_crud (article.tests.ArticleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/nurzhan/CA/article/tests.py", line 55, in test_article_crud
self.assertEqual(Article.objects.all().count(), 1)
AssertionError: 0 != 1
======================================================================
FAIL: test_article_form_valid (article.tests.ArticleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/nurzhan/CA/article/tests.py", line 117, in test_article_form_valid
self.assertTrue(form.is_valid())
AssertionError: False is not true