Как я могу написать модульный тест для представления на основе класса в django? - PullRequest
0 голосов
/ 05 марта 2020

Я новичок в Django разработке.

Скажем, у меня в приложении есть просмотр страницы ApplicationForm следующим образом

class Application(View):
    def get(self, request, *args, **kwargs):
        ...
        ...
        ...
        response = TemplateResponse(request, self.template_name, self.context)
        response.delete_cookie(conf.TRANSACTION_COOKIE_NAME)
        return response

    def post(self, request, *args, **kwargs):
        ...
        ...
        ...
        context.update({
            'error': 1,
            'error_status': de_status_id,
            'error_msg': error_msg,
            'channel': channel,
            'form': self.get_form_for_channel(channel)(self.post_params, channel=channel),
        })
        return render(request, get_landingpage_template(channel, request.path, version), context)

Как мне написать модульный тест для этого, используя django .test

Я попробовал

class BaseViewTest(object):
    longMessage = True  # More verbose messages
    view_class = None

    def setUp(self):
        self.client = Client()

    def tearown(self):
        del self.client

    def is_callable(self, path):
        resp = self.client.get('/{0}'.format(path))
        self.assertEqual(resp.status_code, 200)

    def is_returning_correct_view(self, path, view):
        resp = resolve('/{0}'.format(path))
        self.assertEqual(resp.func.__name__, view)

    def is_correct_template(self, path, template):
        resp = self.client.get('/{0}'.format(path))
        self.assertTemplateUsed(resp, template)

А также модульный тест для проверки правильности шаблона

class TestApplicationPageView(BaseViewTest, SimpleTestCase):
    def test_application(self):
        self.is_callable('apply/')
        self.is_returning_correct_view('apply/', ApplicationPageHandler.as_view().__name__)
        self.is_correct_template('apply/', 'application.html')

Как мне написать блок тесты для методов get и post?

...