0

I need some help with my testing architecture. My code works but it seems ugly for me. Could you take a look :

The goal is to make the same tests for all my pages in Django :

I wrote a unit testing.py

from django.urls import reverse, resolve

class SinglePageTest(object):
    str_reverse = ''
    adresse = ''
    template = None
    template_path = ''
    str_contain = ''

    def initialize(self):
        self.url = reverse(self.str_reverse)
        self.view = resolve(self.adresse)
        self.response = self.client.get(self.url)

    def test_status_200(self):
        self.assertEqual(self.response.status_code, 200)

    def test_templates_home(self):
        self.assertTemplateUsed(self.response, self.template_path)

    def test_contains_HTML(self):
        self.assertContains(self.response,
                            self.str_contain)

    def test_url_resolve(self):
        self.assertEqual(self.view.func.__name__,
                         self.template.as_view().__name__)

When I need to test a page in test.py I do this :

from django.test import TestCase, SimpleTestCase
from DjangoTools.testing import SinglePageTest

class RetrievePassword(SimpleTestCase, SinglePageTest):
    def setUp(self):
        self.str_reverse = 'retrieve-password'
        self.adresse = '/accounts/retrieve-password'
        self.template = RetrievePasswordView
        self.template_path = 'accounts/retrieve-password.html'
        self.str_contain = '<h1> Récupérer le <span class="clr-org">mot de passe</span></h1>'
        super(RetrievePassword, self).setUp()
        SinglePageTest.initialize(self)

The problem is that PyCharm doesn't find the reference for a lot of method in testing.py and that's normal cause I'am using a basic object that doesn't contains these methods.

My questions are :

  1. Is-it well to do like this ?
  2. Can I say that I'm using Mixins ?
  3. How to tell pycharm to find assertTemplateUsed, client.get etc...

Have a nice day,


After using the solution provided unittest is trying to test the testing.py class

Problem


Solution of the problem : add to the base class

def setUp(self):
    if self.__class__ is BaseSinglePageTest:
        raise SkipTest("%s is an abstract base class" % self.__class__)
    else:
        super(BaseSinglePageTest, self).setUp()
mevaereR
  • 63
  • 9

2 Answers2

0

Instead of creating mixins, linear inheritance should work nice on this case.

SimpleTestCase <- SinglePageTest <- RetrievePassword
from django.urls import reverse, resolve

class BaseSinglePageTest(SimpleTestCase):
    str_reverse = ''
    adresse = ''
    template = None
    template_path = ''
    str_contain = ''

    def initialize(self):
        self.url = reverse(self.str_reverse)
        self.view = resolve(self.adresse)
        self.response = self.client.get(self.url)

    def test_status_200(self):
        self.assertEqual(self.response.status_code, 200)

    def test_templates_home(self):
        self.assertTemplateUsed(self.response, self.template_path)

    def test_contains_HTML(self):
        self.assertContains(self.response,
                            self.str_contain)

    def test_url_resolve(self):
        self.assertEqual(self.view.func.__name__,
                         self.template.as_view().__name__)

and then use this class like so

from django.test import TestCase, SimpleTestCase
from DjangoTools.testing import SinglePageTest

class RetrievePassword(BaseSinglePageTest):
    def setUp(self):
        self.str_reverse = 'retrieve-password'
        self.adresse = '/accounts/retrieve-password'
        self.template = RetrievePasswordView
        self.template_path = 'accounts/retrieve-password.html'
        self.str_contain = '<h1> Récupérer le <span class="clr-org">mot de passe</span></h1>'
        super(RetrievePassword, self).setUp()
        SinglePageTest.initialize(self)
Kryštof Řeháček
  • 1,965
  • 1
  • 16
  • 28
0

testing.py

from django.test import SimpleTestCase from django.urls import reverse, resolve from unittest import SkipTest

class BaseSinglePageTest(SimpleTestCase):
    str_reverse = ''
    adresse = ''
    template = None
    template_path = ''
    str_contain = ''

    def setUp(self):
        if self.__class__ is BaseSinglePageTest:
            raise SkipTest("%s is an abstract base class" % self.__class__)
        else:
            super(BaseSinglePageTest, self).setUp()

    def initialize(self):
        self.url = reverse(self.str_reverse)
        self.view = resolve(self.adresse)
        self.response = self.client.get(self.url)

    def test_status_200(self):
        self.assertEqual(self.response.status_code, 200)

    def test_templates_home(self):
        self.assertTemplateUsed(self.response, self.template_path)

    def test_contains_HTML(self):
        self.assertContains(self.response,
                            self.str_contain)

    def test_url_resolve(self):
        self.assertEqual(self.view.func.__name__,
                         self.template.as_view().__name__)

test.py

from django.contrib.auth import get_user_model, authenticate

from DjangoTools.tool_test import BaseSinglePageTest

class RetrievePassword(BaseSinglePageTest):
    def setUp(self):
        self.str_reverse = 'retrieve-password'
        self.adresse = '/accounts/retrieve-password'
        self.template = RetrievePasswordView
        self.template_path = 'accounts/retrieve-password.html'
        self.str_contain = '<h1> Récupérer le <span class="clr-org">mot de passe</span></h1>'
        super(RetrievePassword, self).setUp()
        BaseSinglePageTest.initialize(self)
mevaereR
  • 63
  • 9