From 6bc7c03c1fa9c289e4fff457e5404bffbb595fe7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Perrin?= Date: Fri, 4 Nov 2016 19:35:37 +0000 Subject: [PATCH] Convert to using pytest --- quotes/conftest.py | 31 +++++++++ quotes/test_pyflakes.py | 19 +++--- quotes/test_quotes.py | 147 ++++++++++++++++++---------------------- quotes/test_search.py | 23 ++----- 4 files changed, 113 insertions(+), 107 deletions(-) create mode 100644 quotes/conftest.py diff --git a/quotes/conftest.py b/quotes/conftest.py new file mode 100644 index 0000000..9e88dc0 --- /dev/null +++ b/quotes/conftest.py @@ -0,0 +1,31 @@ +import pytest + +import lxml.etree + +class ValidatingClient(object): + def __init__(self, client): + self.client = client + + def request(self, url, method, exp_status=200, params={}): + if method == 'get': + response = self.client.get('/quotes/' + url) + elif method == 'post': + response = self.client.post('/quotes/' + url, params) + else: + raise RuntimeError('Unknown method %s for %s' % (method, url)) + assert response.status_code == exp_status + assert response.charset == 'utf-8' + document = response.content.decode(response.charset) + lxml.etree.fromstring(document) + assert '") w1 = Work.objects.create(name="Context with some notes", @@ -53,21 +37,22 @@ class ViewsTest(TestCase): a2 = Author.objects.create(name="Author without notes") w2 = Work.objects.create(name="Work without notes", author=a2) q2 = Quote.objects.create(text="

Quote02, no tags

", work=w2) - self.assertSequenceEqual(q2.tags.all(), []) + assert len(q2.tags.all()) == 0 - def test_all(self): - content = self.getPage('all/') + def test_all(self, c): + content = c.getPage('all/') for a in Author.objects.all(): - self.assertIn(a.name, content) - self.assertIn(a.notes, content) + assert a.name in content + assert a.notes in content for w in Work.objects.all(): - self.assertIn(w.name, content) - self.assertIn(w.notes, content) + assert w.name in content + assert w.notes in content for q in Quote.objects.all(): - self.assertIn(q.text, content) - self.assertIn(q.notes, content) + assert q.text in content + assert q.notes in content - def test_random(self): + @pytest.mark.slow + def test_random(self, c): seen = {} for q in Quote.objects.all(): seen[q.id] = False @@ -76,82 +61,82 @@ class ViewsTest(TestCase): permalinkre = re.compile(r'([0-9]+).*Permalink') for i in range(100): - content = self.getPage('random') + content = c.getPage('random') m = permalinkre.search(content) - self.assertIsNotNone(m, content) + assert m is not None, "Did not find the permalink in: "+content quoteid = int(m.group(1)) - self.assertIn(quoteid, seen) + assert quoteid in seen seen[quoteid] = True for q in Quote.objects.all(): - self.assertTrue(seen[q.id]) + assert seen[q.id] - def test_views_all_data(self): + def test_views_all_data(self, c): q = Quote.objects.filter(text__startswith="

Quote01") - self.assertEqual(q.count(), 1, "Couldn't find Quote01 after insertion") + assert q.count() == 1, "Couldn't find Quote01 after insertion" q = q[0] # check the individual quote page; each of the note type is displayed - quotepage = self.getPage('show/%s/' % q.id) - self.assertIn(q.text, quotepage) - self.assertIn("author_notes", quotepage) - self.assertIn("work_notes", quotepage) - self.assertIn("tag_link", quotepage) - self.assertEqual(quotepage.count("tag_link"), 2) + quotepage = c.getPage('show/%s/' % q.id) + assert q.text in quotepage + assert "author_notes" in quotepage + assert "work_notes" in quotepage + assert "tag_link" in quotepage + assert quotepage.count("tag_link") == 2 # check the work page; each of the note type is displayed, and # the work notes are shown only once - workpage = self.getPage('work/%s/' % q.work.id) - self.assertIn(q.text, workpage) - self.assertIn("author_notes", workpage) - self.assertIn("work_notes", workpage) - self.assertEqual(workpage.count("work_notes"), 1) - self.assertIn("tag_link", workpage) + workpage = c.getPage('work/%s/' % q.work.id) + assert q.text in workpage + assert "author_notes" in workpage + assert "work_notes" in workpage + assert workpage.count("work_notes") == 1 + assert "tag_link" in workpage # check the author page; each of the note type is displayed, and # the author notes are shown only once - authorpage = self.getPage('author/%s/' % q.work.author.id) - self.assertIn(q.text, authorpage) - self.assertIn("author_notes", authorpage) - self.assertEqual(authorpage.count("author_notes"), 1) - self.assertIn("work_notes", authorpage) - self.assertIn("tag_link", authorpage) + authorpage = c.getPage('author/%s/' % q.work.author.id) + assert q.text in authorpage + assert "author_notes" in authorpage + assert authorpage.count("author_notes") == 1 + assert "work_notes" in authorpage + assert "tag_link" in authorpage # check the tag page for tag in q.tags.all(): - tagpage = self.getPage('tag/%s/' % tag.id) - self.assertIn(q.text, tagpage) + tagpage = c.getPage('tag/%s/' % tag.id) + assert q.text in tagpage - def test_views_minimal_data(self): + def test_views_minimal_data(self, c): q = Quote.objects.filter(text__startswith="

Quote02") - self.assertEqual(q.count(), 1, - "Couldn't find Quote02 after insertion") + assert q.count() == 1, \ + "Couldn't find Quote02 after insertion" q = q[0] - quotepage = self.getPage('show/%s/' % q.id) - self.assertIn(q.text, quotepage) - self.assertNotIn("author_notes", quotepage) - self.assertNotIn("work_notes", quotepage) - self.assertNotIn("tag_link", quotepage) - - workpage = self.getPage('work/%s/' % q.work.id) - self.assertIn(q.text, workpage) - self.assertNotIn("author_notes", quotepage) - self.assertNotIn("work_notes", quotepage) - self.assertNotIn("tag_link", quotepage) - - authorpage = self.getPage('author/%s/' % q.work.author.id) - self.assertIn(q.text, authorpage) - self.assertNotIn("author_notes", authorpage) - self.assertNotIn("work_notes", authorpage) - self.assertNotIn("tag_link", authorpage) - self.assertNotIn('Quote01, two tags', authorpage) - - def test_view_author_notes_once(self): + quotepage = c.getPage('show/%s/' % q.id) + assert q.text in quotepage + assert "author_notes" not in quotepage + assert "work_notes" not in quotepage + assert "tag_link" not in quotepage + + workpage = c.getPage('work/%s/' % q.work.id) + assert q.text in workpage + assert "author_notes" not in quotepage + assert "work_notes" not in quotepage + assert "tag_link" not in quotepage + + authorpage = c.getPage('author/%s/' % q.work.author.id) + assert q.text in authorpage + assert "author_notes" not in authorpage + assert "work_notes" not in authorpage + assert "tag_link" not in authorpage + assert 'Quote01, two tags' not in authorpage + + def test_view_author_notes_once(self, c): # check that on the per-author view, the author notes aren't display # for every quote a = Author.objects.filter(name="Author with notes") - self.assertEqual(a.count(), 1) + assert a.count() == 1 a = a[0] - authorpage = self.getPage('author/%s/' % a.id) - self.assertEqual(authorpage.count("Some notes for the author"), 1) + authorpage = c.getPage('author/%s/' % a.id) + assert authorpage.count("Some notes for the author") == 1 diff --git a/quotes/test_search.py b/quotes/test_search.py index 3a06d3e..49b59da 100644 --- a/quotes/test_search.py +++ b/quotes/test_search.py @@ -1,10 +1,6 @@ -from django.test import Client - -# Create your tests here. import pytest from .models import Author, Work, Quote -import lxml.etree class Test_Search(): @pytest.fixture(scope='function') @@ -14,19 +10,12 @@ class Test_Search(): q1 = Quote.objects.create(text="

Ich bin...

", work=w1) return q1 - def postPage(self, url, params, exp_status=200): - c = Client() - response = c.post('/quotes/' + url, params) - assert response.status_code == 200 - assert response.charset == 'utf-8' - document = response.content.decode(response.charset) - lxml.etree.fromstring(document) - assert '