python - How fix error in unit test? | AssertionError -
i trying write unit-test
create function
in django project. first experience creating of unit-tests. why cant create new data? error understand there no articles in test database. did mistake?
tests.py:
class articleviewtestcase(testcase): def setup(self): self.user = user.objects.create( username='user', email='user@gmail.com', 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) open('/home/nurzhan/downloads/planet.jpg', 'rb') 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 not callable
you can override form_invalid
, check data in test
class articlecreateview(createview): # 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') 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' )
Comments
Post a Comment