Reputation: 5246
I am trying to write unit-test
to my create function
in Django project. This is my first experience creating of unit-tests. Why I cant create new data? From error I understand that there is no articles in test database. Where I did mistake?
tests.py:
class ArticleViewTestCase(TestCase):
def setUp(self):
self.user = User.objects.create(
username='user',
email='[email protected]',
password='password'
)
self.client = Client()
def test_article_create(self):
self.assertTrue(self.user)
self.client.login(username='user', password='password')
response = self.client.get('/article/create/')
self.assertEquals(response.status_code, 200)
with open('/home/nurzhan/Downloads/planet.jpg', 'rb') as image:
imageStringIO = StringIO(image.read()) # <-- ERROR HERE
response = self.client.post(
'/article/create/',
content_type='multipart/form-data',
data={
'head': 'TEST',
'opt_head': 'TEST',
'body': 'TEST',
'location': 1,
'public': True,
'idx': 0,
'image': (imageStringIO, 'image.jpg')
},
follow=True
)
self.assertEqual(response.status_code, 200)
self.assertEqual(Article.objects.all().count(), 1)
ERROR:
Traceback (most recent call last):
File "/home/nurzhan/CA/article/tests.py", line 26, in test_article_create
imageStringIO = StringIO(image.read())
TypeError: 'module' object is not callable
Upvotes: 1
Views: 7878
Reputation:
You can override form_invalid
and check it data in your test
class ArticleCreateView(CreateView):
# YOUR code here
def form_invalid(self, form):
data = {'status': False, 'errors': form.errors}
return JsonResponse(data, , status=500)
in test method:
with open('/home/nurzhan/Downloads/planet.jpg', 'rb') as image:
response = self.client.post('/article/create/',
data={
'head': 'TEST',
'opt_head': 'TEST',
'body': 'TEST',
'location': 1,
'public': True,
'idx': 0,
'image': image
},
follow=True, format='multipart'
)
Upvotes: 2
Reputation: 6949
When unit testing, Django empties the database at the end of each test, so each test starts with an empty database. It is very likely that no user exists at the beginning of your test, which means login is failing. You should change the test to something like this:
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
class ArticleViewTestCase(TestCase):
def test_article_create(self):
User.objects.create(username='alice', password=make_password('topsecret'))
logged_in = self.client.login(username='alice', password="topsecret")
self.assertTrue(logged_in)
# Continue your test here
After you get a grip on that, you will eventually want to move the user creation to the setUp()
method.
Upvotes: 1
Reputation: 1245
Insert to your test code PDB test tool:
# Below response = self.client.get('/article/create/')
import pdb;pdb.set_trace()
And check response.data
there will be answer for you what is going on there :)
Some tips to your test code:
If u don't need check user login data then use
self.client.force_login(self.user)
you need give only user objects and its clear.
differents
U can split get and post to different test methods its good when u have a lot of https methods to test but its optional - only suggest
Try don't use len()
on your objects from db just use
Article.objects.all().count()
to get numbers of objects
then u dont need use len()
.
articles_amount = Article.objects.all().count()
self.assertEqual(articles_amount, 1)
Upvotes: 0