Skip to content

Instantly share code, notes, and snippets.

@pyjavo
Created April 11, 2018 21:45
Show Gist options
  • Select an option

  • Save pyjavo/1c3080db18d1104795e1420dbefd1640 to your computer and use it in GitHub Desktop.

Select an option

Save pyjavo/1c3080db18d1104795e1420dbefd1640 to your computer and use it in GitHub Desktop.

Revisions

  1. pyjavo created this gist Apr 11, 2018.
    223 changes: 223 additions & 0 deletions test_driven_development_django.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,223 @@
    # Test-Driven Django Development



    ## Our first test: __str__ method


    ```
    from django.test import TestCase
    from .models import Entry
    class EntryModelTest(TestCase):
    def test_string_representation(self):
    entry = Entry(title="My entry title")
    self.assertEqual(str(entry), entry.title)
    ```


    ## Test pluralization


    ```
    def test_verbose_name_plural(self):
    self.assertEqual(str(Entry._meta.verbose_name_plural), "entries")
    ```


    ## The homepage test


    ```
    class ProjectTests(TestCase):
    def test_homepage(self):
    response = self.client.get('/')
    self.assertEqual(response.status_code, 200)
    ```



    ## 404 test en WMS_IQ project


    ```
    def test_judge_list_user_has_none_agency_returns_404(self):
    url = reverse('agency:judge_list')
    self.user.agency = None
    self.user.save()
    response = self.client.get(url)
    self.assertEqual(response.status_code, 404)
    ```


    ## Testing entries

    ```
    from django.contrib.auth import get_user_model
    class HomePageTests(TestCase):
    """Test whether our blog entries show up on the homepage"""
    def setUp(self):
    self.user = get_user_model().objects.create(username='some_user')
    def test_one_entry(self):
    Entry.objects.create(title='1-title', body='1-body', author=self.user)
    response = self.client.get('/')
    self.assertContains(response, '1-title')
    self.assertContains(response, '1-body')
    def test_two_entries(self):
    Entry.objects.create(title='1-title', body='1-body', author=self.user)
    Entry.objects.create(title='2-title', body='2-body', author=self.user)
    response = self.client.get('/')
    self.assertContains(response, '1-title')
    self.assertContains(response, '1-body')
    self.assertContains(response, '2-title')
    ```

    ## Testing no entries

    ```
    def test_no_entries(self):
    response = self.client.get('/')
    self.assertContains(response, 'No blog entries yet.')
    ```


    ## Contiene contenido

    ```
    def test_judge_create_displays_form(self):
    url = reverse('agency:judge_create')
    response = self.client.get(url)
    self.assertContains(response, 'name="first_name"')
    self.assertContains(response, 'name="last_name"')
    self.assertContains(response, 'name="middle_name"')
    self.assertContains(response, 'name="status"')
    ```




    #### Testing Forms




    ## Ensure that form.is_valid() is True. // Ensure that form.is_valid() is False


    ```
    def test_valid_data(self):
    form = CommentForm({
    'name': "Turanga Leela",
    'email': "[email protected]",
    'body': "Hi there",
    }, entry=self.entry)
    self.assertTrue(form.is_valid())
    comment = form.save()
    self.assertEqual(comment.name, "Turanga Leela")
    self.assertEqual(comment.email, "[email protected]")
    self.assertEqual(comment.body, "Hi there")
    self.assertEqual(comment.entry, self.entry)
    def test_blank_data(self):
    form = CommentForm({}, entry=self.entry)
    self.assertFalse(form.is_valid())
    self.assertEqual(form.errors, {
    'name': ['required'],
    'email': ['required'],
    'body': ['required'],
    })
    ```

    *The Django test client cannot test form submissions, but WebTest can. We’ll use django-webtest to handle testing the form submission.*

    ```
    from django_webtest import WebTest
    class EntryViewTest(WebTest):
    def test_view_page(self):
    page = self.app.get(self.entry.get_absolute_url())
    self.assertEqual(len(page.forms), 1)
    ```


    ## Testing sending

    ```
    def test_form_error(self):
    page = self.app.get(self.entry.get_absolute_url())
    page = page.form.submit()
    self.assertContains(page, "This field is required.")
    def test_form_success(self):
    page = self.app.get(self.entry.get_absolute_url())
    page.form['name'] = "Phillip"
    page.form['email'] = "[email protected]"
    page.form['body'] = "Test comment body."
    page = page.form.submit()
    self.assertRedirects(page, self.entry.get_absolute_url())
    ```


    *Coverage, a tool that measures code coverage for Python code, will be used to check what percentage of the tutorial code is being tested.*

    *First install coverage with pip*
    `$ coverage run --include='./*' manage.py test`

    *Prettify the coverage report*
    `coverage html`

    *Branch coverage*
    ```
    coverage run --include='./*' --branch manage.py test
    coverage report
    ```

    *Coverage configuration (.coveragerc)*

    ```
    [run]
    include = ./*
    branch = True
    ```

    *Now we can run -->*
    ```
    coverage run manage.py test
    coverage html
    ```

    ## Testing gravatar images

    ```
    def test_gravatar_url(self):
    comment = Comment(body="My comment body", email="[email protected]")
    expected = "http://www.gravatar.com/avatar/5658ffccee7f0ebfda2b226238b1eb6e"
    self.assertEqual(comment.gravatar_url(), expected)
    ```





    Averiguar sobre BDD
    (Behavioral-Driven development)