{"title":"Do chéad aip Django a scríobh, cuid 5","version":"5.2","locale":"ga","docname":"intro/tutorial05","url":"/ga/5.2/intro/tutorial05/","canonical":"https://djangodocs.dev/ga/5.2/intro/tutorial05/","summary":"Tosaíonn an rang teagaisc seo nuair a d'fhág Tutorial 4 as. Tá feidhmchlár vótaíochta gréasáin tógtha againn, agus cruthóimid roinnt tástálacha uathoibrithe dó…","html":"<h1>Do chéad aip Django a scríobh, cuid 5<a class=\"heading-anchor\" href=\"#writing-your-first-django-app-part-5\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Tosaíonn an rang teagaisc seo nuair a d'fhág <a class=\"reference internal\" href=\"/ga/5.2/intro/tutorial04/\"><span class=\"doc\">Tutorial 4</span></a> as. Tá feidhmchlár vótaíochta gréasáin tógtha againn, agus cruthóimid roinnt tástálacha uathoibrithe dó anois.</p>\n<aside class=\"admonition-where-to-get-help admonition\">\n<p class=\"admonition-title\">Cá háit le cabhair a fháil:</p>\n<p>Má tá deacracht agat dul tríd an rang teagaisc seo, téigh ar aghaidh chuig an rannán :d oc:'Getting Help&lt;/faq/help&gt;' den rannán Ceisteanna Coitianta.</p>\n</aside>\n<section id=\"introducing-automated-testing\">\n<h2>Tástáil uathoibrithe a thabhairt isteach<a class=\"heading-anchor\" href=\"#introducing-automated-testing\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"what-are-automated-tests\">\n<h3>Cad is tástálacha uathoibrithe ann?<a class=\"heading-anchor\" href=\"#what-are-automated-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Is gnáthaimh iad tástálacha a sheiceálann oibriú do chóid.</p>\n<p>Feidhmíonn an tástáil ag leibhéil éagsúla. B’fhéidir go mbainfeadh roinnt tástálacha le mionsonra beag (<em>an dtugann modh múnla ar leith luachanna mar a bhíothas ag súil leis?</em>) agus scrúdaíonn cinn eile oibriú iomlán na mbogearraí (<em>an dtugann seicheamh ionchuir úsáideora ar an suíomh an toradh inmhianaithe?</em>). Níl sé sin difriúil ón gcineál tástála a rinne tú níos luaithe i <a class=\"reference internal\" href=\"/ga/5.2/intro/tutorial02/\"><span class=\"doc\">Teagasc 2</span></a>, ag baint úsáide as an <a class=\"reference internal\" href=\"/ga/5.2/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">shell</span></code></a> chun iompar modha a scrúdú, nó an feidhmchlár a rith agus sonraí a chur isteach chuig seiceáil conas a iompraíonn sé.</p>\n<p>Is é an rud atá difriúil i dtrialacha <em>uathoibrithe</em> ná go ndéanann an córas an obair thástála duitse. Cruthaíonn tú sraith tástálacha uair amháin, agus ansin de réir mar a dhéanann tú athruithe ar d’aip, is féidir leat a sheiceáil go n-oibríonn do chód fós mar a bhí beartaithe agat ar dtús, gan tástáil láimhe am-íditheach a dhéanamh.</p>\n</section>\n<section id=\"why-you-need-to-create-tests\">\n<h3>Cén fáth ar gá duit tástálacha a chruthú<a class=\"heading-anchor\" href=\"#why-you-need-to-create-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Mar sin cén fáth tástálacha a chruthú, agus cén fáth anois?</p>\n<p>B'fhéidir go mbraitheann tú go bhfuil do dhóthain ar do phláta díreach ag foghlaim Python/Django, agus b'fhéidir go bhfuil cuma ró-mhór agus b'fhéidir nach bhfuil gá le rud eile le foghlaim agus le déanamh. Tar éis an tsaoil, tá ár n-iarratas vótaíochta ag obair go sona sásta anois; ní éireoidh níos fearr leis an deacracht a bhaineann le tástálacha uathoibrithe a chruthú. Más é cruthú an fheidhmchláir vótaíochta an píosa deireanach de ríomhchlárú Django a dhéanfaidh tú riamh, mar sin de, ní gá go mbeadh a fhios agat conas tástálacha uathoibrithe a chruthú. Ach, murab é sin an cás, is am iontach é anois le foghlaim.</p>\n<section id=\"tests-will-save-you-time\">\n<h4>Sábhálfaidh tástálacha am duit<a class=\"heading-anchor\" href=\"#tests-will-save-you-time\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Go dtí pointe áirithe, beidh 'seiceáil go ndealraíonn sé go n-oibríonn sé' ina thástáil shásúil. I bhfeidhmchlár níos sofaisticiúla, b'fhéidir go mbeadh mórán idirghníomhaíochtaí casta agat idir comhpháirteanna.</p>\n<p>D’fhéadfadh iarmhairtí gan choinne a bheith ag athrú ar aon cheann de na comhpháirteanna sin ar iompar an fheidhmchláir. D'fhéadfadh sé go gciallódh seiceáil go bhfuil an chuma air go bhfuil sé fós ag obair trí fheidhmiúlacht do chóid le fiche athrú éagsúil ar do shonraí tástála chun a chinntiú nach bhfuil rud éigin briste agat - ní úsáid mhaith é do chuid ama.</p>\n<p>Tá sé sin fíor go háirithe nuair a d'fhéadfadh tástálacha uathoibrithe é seo a dhéanamh duit i soicindí. Má tá rud éigin mícheart, cabhróidh tástálacha freisin chun an cód is cúis leis an iompar gan choinne a aithint.</p>\n<p>Uaireanta, b’fhéidir go mbeadh an chuma air go mbeadh sé ina chontúirt tú féin a stróiceadh ó do chuid oibre táirgiúla, chruthaitheacha ar chlársceidealú chun aghaidh a thabhairt ar ghnó neamhghlamourous unexciting na dtrialacha scríbhneoireachta, go háirithe nuair a bhíonn a fhios agat go bhfuil do chód ag oibriú i gceart.</p>\n<p>Mar sin féin, tá an tasc a bhaineann le tástálacha a scríobh i bhfad níos sásúla ná uaireanta a chaitheamh ag tástáil d'iarratas de láimh nó ag iarraidh cúis na faidhbe nua-thagtha a aithint.</p>\n</section>\n<section id=\"tests-don-t-just-identify-problems-they-prevent-them\">\n<h4>Ní hamháin go n-aithníonn tástálacha fadhbanna, cuireann siad cosc ​​orthu<a class=\"heading-anchor\" href=\"#tests-don-t-just-identify-problems-they-prevent-them\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Is botún é smaoineamh ar thástálacha mar ghné dhiúltach den fhorbairt amháin.</p>\n<p>Gan tástálacha, d’fhéadfadh cuspóir nó iompar beartaithe feidhmchlár a bheith sách teimhneach. Fiú nuair is é do chód féin é, uaireanta beidh tú ag sníomh timpeall ann ag iarraidh a fháil amach cad go díreach atá ar siúl aige.</p>\n<p>Athraíonn tástálacha go; lasann siad do chód ón taobh istigh, agus nuair a théann rud éigin mícheart, díríonn siad solas ar an gcuid a chuaigh mícheart - <em>fiú murar thuig tú fiú go raibh sé imithe amú</em>.</p>\n</section>\n<section id=\"tests-make-your-code-more-attractive\">\n<h4>Déanann tástálacha do chód níos tarraingtí<a class=\"heading-anchor\" href=\"#tests-make-your-code-more-attractive\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>B'fhéidir gur chruthaigh tú píosa bogearraí iontach, ach gheobhaidh tú go ndiúltóidh go leor forbróirí eile breathnú air toisc nach bhfuil tástálacha ann; gan tástálacha, ní bheidh muinín acu ann. Deir Jacob Kaplan-Moss, ceann de na forbróirí bunaidh Django, &quot;Tá Cód gan tástálacha briste de réir dearadh.&quot;</p>\n<p>Is cúis eile fós é gur mian le forbróirí eile tástálacha a fheiceáil i do bhogearraí sula nglacfaidh siad dáiríre é chun tús a chur le tástálacha a scríobh.</p>\n</section>\n<section id=\"tests-help-teams-work-together\">\n<h4>Cuidíonn tástálacha le foirne oibriú le chéile<a class=\"heading-anchor\" href=\"#tests-help-teams-work-together\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Scríobhtar na pointí roimhe seo ó thaobh forbróra amháin a choinníonn iarratas. Coinneoidh foirne iarratais chasta. Cinntíonn tástálacha nach mbriseann comhghleacaithe do chód de thaisme (agus nach mbriseann tú a gcód i ngan fhios). Más mian leat slí bheatha a dhéanamh mar ríomhchláraitheoir Django, caithfidh tú a bheith go maith ag scríobh trialacha!</p>\n</section>\n</section>\n</section>\n<section id=\"basic-testing-strategies\">\n<h2>Straitéisí bunúsacha tástála<a class=\"heading-anchor\" href=\"#basic-testing-strategies\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Tá go leor bealaí ann le tabhairt faoi thrialacha scríbhneoireachta.</p>\n<p>Leanann roinnt ríomhchláraitheoirí disciplín ar a dtugtar &quot;<a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Test-driven_development\">forbairt bunaithe ar thástáil</a>&quot;; scríobhann siad a gcuid tástálacha sula scríobhann siad a gcód. D'fhéadfadh sé seo cosúil go míthuigthe, ach i ndáiríre tá sé cosúil leis an méid a bheidh daoine is minic a dhéanamh ar aon nós: siad cur síos ar fhadhb, ansin a chruthú roinnt cód chun é a réiteach. Déanann forbairt tástála-tiomáinte an fhadhb a fhoirmiú i gcás tástála Python.</p>\n<p>Níos minice, cruthóidh núíosach sa tástáil roinnt cód agus cinnfidh sé níos déanaí gur chóir go mbeadh roinnt tástálacha ann. B'fhéidir go mbeadh sé níos fearr roinnt tástálacha a scríobh níos luaithe, ach ní bhíonn sé ró-dhéanach chun tús a chur leis.</p>\n<p>Uaireanta bíonn sé deacair a dhéanamh amach cá háit le tosú ar thrialacha scríbhneoireachta. Má tá na mílte línte de Python scríofa agat, seans nach mbeadh sé éasca rud éigin a thástáil. I gcás den sórt sin, tá sé torthúil do chéad thástáil a scríobh an chéad uair eile a dhéanann tú athrú, nuair a chuireann tú gné nua leis nó nuair a shocraíonn tú fabht.</p>\n<p>Mar sin déanaimis é sin láithreach.</p>\n</section>\n<section id=\"writing-our-first-test\">\n<h2>Ag scríobh ár gcéad triail<a class=\"heading-anchor\" href=\"#writing-our-first-test\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"we-identify-a-bug\">\n<h3>Aithnímid fabht<a class=\"heading-anchor\" href=\"#we-identify-a-bug\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Ar ámharaí an tsaoil, tá fabht beag san fheidhmchlár <code class=\"docutils literal notranslate\"><span class=\"pre\">vótaíocht</span></code> atá le réiteach againn ar an bpointe boise: foilsíodh an modh <code class=\"docutils literal notranslate\"><span class=\"pre\">Ceist._foilsithe_le</span> <span class=\"pre\">déanaí()</span></code> ar ais <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> má fhoilsítear an <code class=\"docutils literal notranslate\"><span class=\"pre\">Ceist</span></code> laistigh den cheann deireanach lá (atá ceart) ach freisin má tá an réimse <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>'s `` pub_date`` amach anseo (rud nach bhfuil cinnte).</p>\n<p>Deimhnigh an fabht tríd an <a class=\"reference internal\" href=\"/ga/5.2/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">shell</span></code></a> a úsáid chun an modh a sheiceáil ar cheist a bhfuil a dáta sa todhchaí:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>shell\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py shell\n</code></pre></div></div></div>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">datetime</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># create a Question instance with pub_date 30 days in the future</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">future_question</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"p\">(</span><span class=\"n\">pub_date</span><span class=\"o\">=</span><span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span> <span class=\"o\">+</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">timedelta</span><span class=\"p\">(</span><span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">30</span><span class=\"p\">))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># was it published recently?</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">future_question</span><span class=\"o\">.</span><span class=\"n\">was_published_recently</span><span class=\"p\">()</span>\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>Ós rud é nach bhfuil rudaí sa todhchaí 'le déanaí', is léir go bhfuil sé seo mícheart.</p>\n</section>\n<section id=\"create-a-test-to-expose-the-bug\">\n<h3>Cruthaigh tástáil chun an fabht a nochtadh<a class=\"heading-anchor\" href=\"#create-a-test-to-expose-the-bug\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Is é an rud atá díreach déanta againn sa <a class=\"reference internal\" href=\"/ga/5.2/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">shell</span></code></a> chun tástáil a dhéanamh ar an bhfadhb ná an rud go díreach is féidir linn a dhéanamh i dtástáil uathoibrithe, mar sin déanaimis tástáil uathoibrithe air sin.</p>\n<p>Gnátháit do thástálacha feidhmchláir is ea comhad <code class=\"docutils literal notranslate\"><span class=\"pre\">tests.py</span></code> an fheidhmchláir; Gheobhaidh an córas tástála tástálacha go huathoibríoch in aon chomhad a dtosaíonn a ainm le <code class=\"docutils literal notranslate\"><span class=\"pre\">tástáil</span></code>.</p>\n<p>Cuir iad seo a leanas sa chomhad <code class=\"docutils literal notranslate\"><span class=\"pre\">tests.py</span></code> san fheidhmchlár <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/tests.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">datetime</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">TestCase</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">QuestionModelTests</span><span class=\"p\">(</span><span class=\"n\">TestCase</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_was_published_recently_with_future_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        was_published_recently() returns False for questions whose pub_date</span>\n<span class=\"sd\">        is in the future.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">time</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span> <span class=\"o\">+</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">timedelta</span><span class=\"p\">(</span><span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">30</span><span class=\"p\">)</span>\n        <span class=\"n\">future_question</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"p\">(</span><span class=\"n\">pub_date</span><span class=\"o\">=</span><span class=\"n\">time</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertIs</span><span class=\"p\">(</span><span class=\"n\">future_question</span><span class=\"o\">.</span><span class=\"n\">was_published_recently</span><span class=\"p\">(),</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Tá fo-aicme <a class=\"reference internal\" href=\"/ga/5.2/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.test.TestCase</span></code></a> cruthaithe againn anseo le modh a chruthaíonn sampla <code class=\"docutils literal notranslate\"><span class=\"pre\">Ceist</span></code> le <code class=\"docutils literal notranslate\"><span class=\"pre\">dáta_</span> <span class=\"pre\">tábhairne</span></code> sa todhchaí. Déanaimid seiceáil ansin ar aschur <code class=\"docutils literal notranslate\"><span class=\"pre\">foilsíodh_le</span> <span class=\"pre\">déanaí()</span></code> - ar cheart * a bheith Bréagach.</p>\n</section>\n<section id=\"running-tests\">\n<h3>Rith tástálacha<a class=\"heading-anchor\" href=\"#running-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Sa chríochfort, is féidir linn ár dtástáil a rith:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>polls\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py test polls\n</code></pre></div></div></div>\n<p>agus feicfidh tú rud éigin mar:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>Creating<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>database<span class=\"w\"> </span><span class=\"k\">for</span><span class=\"w\"> </span><span class=\"nb\">alias</span><span class=\"w\"> </span><span class=\"s1\">&#39;default&#39;</span>...\nSystem<span class=\"w\"> </span>check<span class=\"w\"> </span>identified<span class=\"w\"> </span>no<span class=\"w\"> </span>issues<span class=\"w\"> </span><span class=\"o\">(</span><span class=\"m\">0</span><span class=\"w\"> </span>silenced<span class=\"o\">)</span>.\n<span class=\"nv\">F</span>\n<span class=\"o\">======================================================================</span>\nFAIL:<span class=\"w\"> </span>test_was_published_recently_with_future_question<span class=\"w\"> </span><span class=\"o\">(</span>polls.tests.QuestionModelTests<span class=\"o\">)</span>\n----------------------------------------------------------------------\nTraceback<span class=\"w\"> </span><span class=\"o\">(</span>most<span class=\"w\"> </span>recent<span class=\"w\"> </span>call<span class=\"w\"> </span>last<span class=\"o\">)</span>:\n<span class=\"w\">  </span>File<span class=\"w\"> </span><span class=\"s2\">&quot;/path/to/djangotutorial/polls/tests.py&quot;</span>,<span class=\"w\"> </span>line<span class=\"w\"> </span><span class=\"m\">16</span>,<span class=\"w\"> </span><span class=\"k\">in</span><span class=\"w\"> </span>test_was_published_recently_with_future_question\n<span class=\"w\">    </span>self.assertIs<span class=\"o\">(</span>future_question.was_published_recently<span class=\"o\">()</span>,<span class=\"w\"> </span>False<span class=\"o\">)</span>\nAssertionError:<span class=\"w\"> </span>True<span class=\"w\"> </span>is<span class=\"w\"> </span>not<span class=\"w\"> </span>False\n\n----------------------------------------------------------------------\nRan<span class=\"w\"> </span><span class=\"m\">1</span><span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span><span class=\"k\">in</span><span class=\"w\"> </span><span class=\"m\">0</span>.001s\n\nFAILED<span class=\"w\"> </span><span class=\"o\">(</span><span class=\"nv\">failures</span><span class=\"o\">=</span><span class=\"m\">1</span><span class=\"o\">)</span>\nDestroying<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>database<span class=\"w\"> </span><span class=\"k\">for</span><span class=\"w\"> </span><span class=\"nb\">alias</span><span class=\"w\"> </span><span class=\"s1\">&#39;default&#39;</span>...\n</code></pre></div>\n<aside class=\"admonition-different-error admonition\">\n<p class=\"admonition-title\">Earráid dhifriúil?</p>\n<p>Más rud é ina ionad sin go bhfuil <code class=\"docutils literal notranslate\"><span class=\"pre\">Earráid</span> <span class=\"pre\">Ainm</span></code> á fháil agat anseo, seans gur chaill tú céim i <a class=\"reference internal\" href=\"/ga/5.2/intro/tutorial02/#tutorial02-import-timezone\"><span class=\"std std-ref\">Cuid 2</span></a> inar chuireamar iompórtálacha de <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime</span></code> agus <code class=\"docutils literal notranslate\"><span class=\"pre\">timezone`</span> <span class=\"pre\">`go</span> <span class=\"pre\">dtí``vótaíocht/samhlacha.py</span></code>. Cóipeáil na hiompórtálacha ón rannán sin, agus bain triail as do thástálacha a rith arís.</p>\n</aside>\n<p>Cad a tharla é seo:</p>\n<ul class=\"simple\">\n<li><p>D'fhéach <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">test</span> <span class=\"pre\">polls</span></code> tástálacha san fheidhmchlár <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code></p></li>\n<li><p>fuair sé fo-aicme den rang <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">:rang:`django.test.TestCase`</span></a></p></li>\n<li><p>chruthaigh sé bunachar sonraí speisialta chun críche tástála</p></li>\n<li><p>d'fhéach sé le haghaidh modhanna tástála - cinn a bhfuil a n-ainmneacha tús le <code class=\"docutils literal notranslate\"><span class=\"pre\">test</span></code></p></li>\n<li><p>i <code class=\"docutils literal notranslate\"><span class=\"pre\">tástáil_foilsithe_le_déanaí_le_cheist_future_</span></code> chruthaigh sé <code class=\"docutils literal notranslate\"><span class=\"pre\">Ceist</span></code> a bhfuil réimse `` pub_date`` 30 lá sa todhchaí</p></li>\n<li><p>... agus úsáid á baint as an modh <code class=\"docutils literal notranslate\"><span class=\"pre\">assertIs()</span></code>, fuair sé amach go dtagann <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> ar ais lena <code class=\"docutils literal notranslate\"><span class=\"pre\">was_published_recently()</span></code>, cé gur theastaigh uainn go dtabharfadh sé <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>False`</p></li>\n</ul>\n<p>Cuireann an tástáil in iúl dúinn cén tástáil ar theip uirthi agus fiú an líne ar ar tharla an teip.</p>\n</section>\n<section id=\"fixing-the-bug\">\n<h3>An fabht a shocrú<a class=\"heading-anchor\" href=\"#fixing-the-bug\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Tá a fhios againn cheana féin cad é an fhadhb: <code class=\"docutils literal notranslate\"><span class=\"pre\">Question.was_published_recently()</span></code> ba cheart <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> a thabhairt ar ais má tá a <code class=\"docutils literal notranslate\"><span class=\"pre\">dpub_date</span></code> amach anseo. Leasaigh an modh i <code class=\"docutils literal notranslate\"><span class=\"pre\">models.py</span></code>, ionas nach dtabharfaidh sé ar ais <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> ach amháin má tá an dáta san am atá caite freisin:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/models.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">was_published_recently</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"n\">now</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span>\n    <span class=\"k\">return</span> <span class=\"n\">now</span> <span class=\"o\">-</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">timedelta</span><span class=\"p\">(</span><span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span> <span class=\"o\">&lt;=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">pub_date</span> <span class=\"o\">&lt;=</span> <span class=\"n\">now</span>\n</code></pre></figure>\n<p>agus rith an triail arís:</p>\n<div class=\"code-block\" data-language=\"pytb\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Pytb</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Pytb code\"><code><span class=\"x\">Creating test database for alias &#39;default&#39;...</span>\n<span class=\"x\">System check identified no issues (0 silenced).</span>\n<span class=\"x\">.</span>\n<span class=\"x\">----------------------------------------------------------------------</span>\n<span class=\"x\">Ran 1 test in 0.001s</span>\n\n<span class=\"x\">OK</span>\n<span class=\"x\">Destroying test database for alias &#39;default&#39;...</span>\n</code></pre></div>\n<p>Tar éis fabht a aithint, scríobhamar tástáil a nochtar é agus cheartaigh muid an fabht sa chód ionas go n-éireoidh linn ár dtástáil.</p>\n<p>D’fhéadfadh go n-imeodh go leor rudaí eile lenár n-iarratas amach anseo, ach is féidir linn a bheith cinnte nach dtabharfaimid an fabht seo isteach arís go neamhaireach, mar go dtabharfaidh rith na tástála rabhadh dúinn láithreach. Is féidir linn a mheas go bhfuil an chuid bheag seo den iarratas pinn síos go sábháilte go deo.</p>\n</section>\n<section id=\"more-comprehensive-tests\">\n<h3>Tástálacha níos cuimsithí<a class=\"heading-anchor\" href=\"#more-comprehensive-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Agus muid anseo, is féidir linn an modh <code class=\"docutils literal notranslate\"><span class=\"pre\">foilsíodh_le</span> <span class=\"pre\">déanaí()</span></code>; go deimhin, bheadh ​​sé ina náire dearfach dá mbeadh fabht amháin tugtha isteach againn agus fabht amháin á réiteach.</p>\n<p>Cuir dhá mhodh tástála eile leis an rang céanna, chun iompar an mhodha a thástáil ar bhealach níos cuimsithí:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/tests.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_was_published_recently_with_old_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    was_published_recently() returns False for questions whose pub_date</span>\n<span class=\"sd\">    is older than 1 day.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"n\">time</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span> <span class=\"o\">-</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">timedelta</span><span class=\"p\">(</span><span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"n\">seconds</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n    <span class=\"n\">old_question</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"p\">(</span><span class=\"n\">pub_date</span><span class=\"o\">=</span><span class=\"n\">time</span><span class=\"p\">)</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertIs</span><span class=\"p\">(</span><span class=\"n\">old_question</span><span class=\"o\">.</span><span class=\"n\">was_published_recently</span><span class=\"p\">(),</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_was_published_recently_with_recent_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    was_published_recently() returns True for questions whose pub_date</span>\n<span class=\"sd\">    is within the last day.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"n\">time</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span> <span class=\"o\">-</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">timedelta</span><span class=\"p\">(</span><span class=\"n\">hours</span><span class=\"o\">=</span><span class=\"mi\">23</span><span class=\"p\">,</span> <span class=\"n\">minutes</span><span class=\"o\">=</span><span class=\"mi\">59</span><span class=\"p\">,</span> <span class=\"n\">seconds</span><span class=\"o\">=</span><span class=\"mi\">59</span><span class=\"p\">)</span>\n    <span class=\"n\">recent_question</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"p\">(</span><span class=\"n\">pub_date</span><span class=\"o\">=</span><span class=\"n\">time</span><span class=\"p\">)</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertIs</span><span class=\"p\">(</span><span class=\"n\">recent_question</span><span class=\"o\">.</span><span class=\"n\">was_published_recently</span><span class=\"p\">(),</span> <span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Agus anois tá trí thástáil againn a dheimhníonn go dtugann <code class=\"docutils literal notranslate\"><span class=\"pre\">Question.was_published_recently()</span></code> luachanna ciallmhara do cheisteanna san am a chuaigh thart, do cheisteanna le déanaí agus don todhchaí.</p>\n<p>Arís, is feidhmchlár íosta é <code class=\"docutils literal notranslate\"><span class=\"pre\">vótaíocht</span></code>, ach cé chomh casta a fhásann sé sa todhchaí agus cibé cód eile a n-idirghníomhaíonn sé leis, tá ráthaíocht éigin againn anois go n-iomprófar an modh a bhfuil trialacha scríofa againn dó ar bhealaí ionchais.</p>\n</section>\n</section>\n<section id=\"test-a-view\">\n<h2>Tástáil radharc<a class=\"heading-anchor\" href=\"#test-a-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Tá an t-iarratas pobalbhreithe cothrom neamh-idirdhealaitheach: foilseoidh sé aon cheist, lena n-áirítear na cinn a bhfuil a réimse <code class=\"docutils literal notranslate\"><span class=\"pre\">pub_date</span></code> suite amach anseo. Ba cheart dúinn é seo a fheabhsú. Ba cheart go gciallódh socrú <code class=\"docutils literal notranslate\"><span class=\"pre\">pub_date</span></code> amach anseo go bhfoilseofar an Cheist ag an nóiméad sin, ach dofheicthe go dtí sin.</p>\n<section id=\"a-test-for-a-view\">\n<h3>Tástáil le haghaidh radharc<a class=\"heading-anchor\" href=\"#a-test-for-a-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Nuair a shocraíomar an fabht thuas, scríobhamar an tástáil ar dtús agus ansin an cód chun é a shocrú. Le fírinne ba shampla é sin d’fhorbairt a bhí tiomáinte ag tástáil, ach is cuma i ndáiríre cén ord ina ndéanaimid an obair.</p>\n<p>In ár gcéad tástáil, dhíríomar go dlúth ar iompar inmheánach an chóid. Don tástáil seo, ba mhaith linn a iompar a sheiceáil mar a bheadh ​​taithí ag úsáideoir air trí bhrabhsálaí gréasáin.</p>\n<p>Sula ndéanaimid iarracht rud ar bith a shocrú, caithfimid féachaint ar na huirlisí atá ar fáil dúinn.</p>\n</section>\n<section id=\"the-django-test-client\">\n<h3>Django cliant tástála<a class=\"heading-anchor\" href=\"#the-django-test-client\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Soláthraíonn Django tástáil <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">:rang:`~django.test.Client`</span></a> chun úsáideoir a idirghníomhaíonn leis an gcód ag an leibhéal radhairc a insamhail. Is féidir linn é a úsáid i <code class=\"docutils literal notranslate\"><span class=\"pre\">tests.py</span></code> nó fiú sa <a class=\"reference internal\" href=\"/ga/5.2/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">shell</span></code></a>.</p>\n<p>Tosóimid arís leis an <a class=\"reference internal\" href=\"/ga/5.2/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">shell</span></code></a>, áit a mbeidh orainn cúpla rud a dhéanamh nach mbeidh riachtanach i <code class=\"docutils literal notranslate\"><span class=\"pre\">tests.py</span></code>. Is é an chéad cheann ná an timpeallacht tástála a shocrú sa <a class=\"reference internal\" href=\"/ga/5.2/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">shell</span></code></a>:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>shell\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py shell\n</code></pre></div></div></div>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">setup_test_environment</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">setup_test_environment</span><span class=\"p\">()</span>\n</code></pre></div>\n<p><a class=\"reference internal\" href=\"/ga/5.2/topics/testing/advanced/#django.test.utils.setup_test_environment\" title=\"django.test.utils.setup_test_environment\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">setup_test_environment()</span></code></a> suiteáil rindreálaí teimpléid a ligfidh dúinn scrúdú a dhéanamh ar roinnt tréithe breise ar fhreagraí ar nós <code class=\"docutils literal notranslate\"><span class=\"pre\">response.context</span></code> nach mbeadh ar fáil murach sin. Tabhair faoi deara nach mbunaíonn an modh seo ** bunachar sonraí tástála, mar sin cuirfear an méid seo a leanas i gcomparáid leis an mbunachar sonraí atá ann cheana féin agus d’fhéadfadh go mbeadh difríocht bheag idir an t-aschur ag brath ar na ceisteanna a chruthaigh tú cheana féin. Seans go bhfaighidh tú torthaí gan choinne mura bhfuil do <code class=\"docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code> i <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code> ceart. Mura cuimhin leat é a shocrú níos luaithe, seiceáil é sula leanann tú ar aghaidh.</p>\n<p>Ansin ní mór dúinn an rang cliant tástála a iompórtáil (níos déanaí i <code class=\"docutils literal notranslate\"><span class=\"pre\">tests.py</span></code> úsáidfimid an rang ::django.test.TestCase, a thagann lena cliant féin, mar sin ní bheidh sé seo ag teastáil) :</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Client</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># create an instance of the client for our use</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">client</span> <span class=\"o\">=</span> <span class=\"n\">Client</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>Agus é sin réidh, is féidir linn iarraidh ar an gcliant roinnt oibre a dhéanamh dúinn:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># get a response from &#39;/&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;/&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">Not Found: /</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># we should expect a 404 from that address; if you instead see an</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># &quot;Invalid HTTP_HOST header&quot; error and a 400 response, you probably</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># omitted the setup_test_environment() call described earlier.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">status_code</span>\n<span class=\"go\">404</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># on the other hand we should expect to find something at &#39;/polls/&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># we&#39;ll use &#39;reverse()&#39; rather than a hardcoded URL</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">reverse</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:index&quot;</span><span class=\"p\">))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">status_code</span>\n<span class=\"go\">200</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">content</span>\n<span class=\"go\">b&#39;\\n    &lt;ul&gt;\\n    \\n        &lt;li&gt;&lt;a href=&quot;/polls/1/&quot;&gt;What&amp;#x27;s up?&lt;/a&gt;&lt;/li&gt;\\n    \\n    &lt;/ul&gt;\\n\\n&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s2\">&quot;latest_question_list&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&lt;QuerySet [&lt;Question: What&#39;s up?&gt;]&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"improving-our-view\">\n<h3>Ár dtuairim a fheabhsú<a class=\"heading-anchor\" href=\"#improving-our-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Léiríonn liosta na pobalbhreithe pobalbhreith nach bhfuil foilsithe fós (i.e. iad siúd a mbeidh <code class=\"docutils literal notranslate\"><span class=\"pre\">dáta_</span> <span class=\"pre\">tábhairne</span></code> acu amach anseo). Déanaimis é sin a shocrú.</p>\n<p>In <a class=\"reference internal\" href=\"/ga/5.2/intro/tutorial04/\"><span class=\"doc\">Teagasc 4</span></a> thugamar isteach radharc rang-bhunaithe, bunaithe ar <a class=\"reference internal\" href=\"/ga/5.2/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ListView</span></code></a>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">IndexView</span><span class=\"p\">(</span><span class=\"n\">generic</span><span class=\"o\">.</span><span class=\"n\">ListView</span><span class=\"p\">):</span>\n    <span class=\"n\">template_name</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;polls/index.html&quot;</span>\n    <span class=\"n\">context_object_name</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;latest_question_list&quot;</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_queryset</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Return the last five published questions.&quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">order_by</span><span class=\"p\">(</span><span class=\"s2\">&quot;-pub_date&quot;</span><span class=\"p\">)[:</span><span class=\"mi\">5</span><span class=\"p\">]</span>\n</code></pre></figure>\n<p>Ní mór dúinn an modh <code class=\"docutils literal notranslate\"><span class=\"pre\">get_queryset()</span></code> a leasú agus é a athrú ionas go seiceálann sé an dáta freisin trí é a chur i gcomparáid le <code class=\"docutils literal notranslate\"><span class=\"pre\">timezone.now()</span></code>. Ar dtús caithfimid allmhairiú a chur leis:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n</code></pre></figure>\n<p>agus ansin caithfimid an modh <code class=\"docutils literal notranslate\"><span class=\"pre\">get_queryset</span></code> a leasú mar seo:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_queryset</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Return the last five published questions (not including those set to be</span>\n<span class=\"sd\">    published in the future).</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">return</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">pub_date__lte</span><span class=\"o\">=</span><span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">())</span><span class=\"o\">.</span><span class=\"n\">order_by</span><span class=\"p\">(</span><span class=\"s2\">&quot;-pub_date&quot;</span><span class=\"p\">)[</span>\n        <span class=\"p\">:</span><span class=\"mi\">5</span>\n    <span class=\"p\">]</span>\n</code></pre></figure>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Question.objects.filter(pub_date__lte=timezone.now())</span></code> returns a queryset\ncontaining <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>s whose <code class=\"docutils literal notranslate\"><span class=\"pre\">pub_date</span></code> is less than or equal to - that\nis, earlier than or equal to - <code class=\"docutils literal notranslate\"><span class=\"pre\">timezone.now()</span></code>.</p>\n</section>\n<section id=\"testing-our-new-view\">\n<h3>Ár radharc nua a thástáil<a class=\"heading-anchor\" href=\"#testing-our-new-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Now you can satisfy yourself that this behaves as expected by firing up\n<code class=\"docutils literal notranslate\"><span class=\"pre\">runserver</span></code>, loading the site in your browser, creating a few <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>\nentries with dates in the past and future, and checking that only those that\nhave been published are listed. You don't want to have to do that <em>every single\ntime you make any change that might affect this</em> - so let's also create a test,\nbased on our <a class=\"reference internal\" href=\"/ga/5.2/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">shell</span></code></a> session above.</p>\n<p>Cuir iad seo a leanas le <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/tests.py</span></code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/tests.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">reverse</span>\n</code></pre></figure>\n<p>agus cruthóimid feidhm aicearra chun ceisteanna a chruthú chomh maith le rang tástála nua:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/tests.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Create a question with the given `question_text` and published the</span>\n<span class=\"sd\">    given number of `days` offset to now (negative for questions published</span>\n<span class=\"sd\">    in the past, positive for questions that have yet to be published).</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"n\">time</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span> <span class=\"o\">+</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">timedelta</span><span class=\"p\">(</span><span class=\"n\">days</span><span class=\"o\">=</span><span class=\"n\">days</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"n\">question_text</span><span class=\"p\">,</span> <span class=\"n\">pub_date</span><span class=\"o\">=</span><span class=\"n\">time</span><span class=\"p\">)</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">QuestionIndexViewTests</span><span class=\"p\">(</span><span class=\"n\">TestCase</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_no_questions</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        If no questions exist, an appropriate message is displayed.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:index&quot;</span><span class=\"p\">))</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">status_code</span><span class=\"p\">,</span> <span class=\"mi\">200</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertContains</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"p\">,</span> <span class=\"s2\">&quot;No polls are available.&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertQuerySetEqual</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s2\">&quot;latest_question_list&quot;</span><span class=\"p\">],</span> <span class=\"p\">[])</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_past_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Questions with a pub_date in the past are displayed on the</span>\n<span class=\"sd\">        index page.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">question</span> <span class=\"o\">=</span> <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Past question.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=-</span><span class=\"mi\">30</span><span class=\"p\">)</span>\n        <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:index&quot;</span><span class=\"p\">))</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertQuerySetEqual</span><span class=\"p\">(</span>\n            <span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s2\">&quot;latest_question_list&quot;</span><span class=\"p\">],</span>\n            <span class=\"p\">[</span><span class=\"n\">question</span><span class=\"p\">],</span>\n        <span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_future_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Questions with a pub_date in the future aren&#39;t displayed on</span>\n<span class=\"sd\">        the index page.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Future question.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">30</span><span class=\"p\">)</span>\n        <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:index&quot;</span><span class=\"p\">))</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertContains</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"p\">,</span> <span class=\"s2\">&quot;No polls are available.&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertQuerySetEqual</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s2\">&quot;latest_question_list&quot;</span><span class=\"p\">],</span> <span class=\"p\">[])</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_future_question_and_past_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Even if both past and future questions exist, only past questions</span>\n<span class=\"sd\">        are displayed.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">question</span> <span class=\"o\">=</span> <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Past question.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=-</span><span class=\"mi\">30</span><span class=\"p\">)</span>\n        <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Future question.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">30</span><span class=\"p\">)</span>\n        <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:index&quot;</span><span class=\"p\">))</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertQuerySetEqual</span><span class=\"p\">(</span>\n            <span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s2\">&quot;latest_question_list&quot;</span><span class=\"p\">],</span>\n            <span class=\"p\">[</span><span class=\"n\">question</span><span class=\"p\">],</span>\n        <span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_two_past_questions</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        The questions index page may display multiple questions.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">question1</span> <span class=\"o\">=</span> <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Past question 1.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=-</span><span class=\"mi\">30</span><span class=\"p\">)</span>\n        <span class=\"n\">question2</span> <span class=\"o\">=</span> <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Past question 2.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=-</span><span class=\"mi\">5</span><span class=\"p\">)</span>\n        <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:index&quot;</span><span class=\"p\">))</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertQuerySetEqual</span><span class=\"p\">(</span>\n            <span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s2\">&quot;latest_question_list&quot;</span><span class=\"p\">],</span>\n            <span class=\"p\">[</span><span class=\"n\">question2</span><span class=\"p\">,</span> <span class=\"n\">question1</span><span class=\"p\">],</span>\n        <span class=\"p\">)</span>\n</code></pre></figure>\n<p>Breathnaímis ar chuid acu seo níos mine.</p>\n<p>Ar dtús tá feidhm aicearra ceisteanna, <code class=\"docutils literal notranslate\"><span class=\"pre\">cruthaigh_cheist</span></code>, chun roinnt athrá a bhaint as an bpróiseas chun ceisteanna a chruthú.</p>\n<p>Ní chruthaíonn <code class=\"docutils literal notranslate\"><span class=\"pre\">test_no_questions</span></code> aon cheist, ach seiceálann sé an teachtaireacht: &quot;Níl aon pobalbhreith ar fáil.&quot; agus fíoraíonn sé go bhfuil <code class=\"docutils literal notranslate\"><span class=\"pre\">liosta_ceisteanna_is</span> <span class=\"pre\">déanaí</span></code> folamh. Tabhair faoi deara go soláthraíonn an rang <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">:rang:`django.test.TestCase`</span></a> roinnt modhanna dearbhaithe breise. Sna samplaí seo, úsáidimid <a class=\"reference internal\" href=\"/ga/5.2/topics/testing/tools/#django.test.SimpleTestCase.assertContains\" title=\"django.test.SimpleTestCase.assertContains\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">assertContains()</span></code></a> agus <a class=\"reference internal\" href=\"/ga/5.2/topics/testing/tools/#django.test.TransactionTestCase.assertQuerySetEqual\" title=\"django.test.TransactionTestCase.assertQuerySetEqual\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">assertQuerySetEqual()</span></code></a>.</p>\n<p>In <code class=\"docutils literal notranslate\"><span class=\"pre\">test_past_question</span></code>, cruthaímid ceist agus deimhnímid go bhfuil sé le feiceáil ar an liosta.</p>\n<p>I <code class=\"docutils literal notranslate\"><span class=\"pre\">test_future_question</span></code>, cruthaímid ceist le <code class=\"docutils literal notranslate\"><span class=\"pre\">dáta_tábhairne</span></code> amach anseo. Athshocraítear an bunachar sonraí do gach modh tástála, mar sin níl an chéad cheist ann a thuilleadh, agus mar sin arís níor cheart go mbeadh aon cheist san innéacs.</p>\n<p>Agus mar sin de. I ndáiríre, táimid ag baint úsáide as na tástálacha chun scéal ionchur admin agus taithí úsáideora ar an suíomh a insint, agus ag seiceáil go bhfuil ag gach stát agus le haghaidh gach athrú nua ar staid an chórais, foilsítear na torthaí ionchais.</p>\n</section>\n<section id=\"testing-the-detailview\">\n<h3>Ag tástáil an <code class=\"docutils literal notranslate\"><span class=\"pre\">DetailView</span></code><a class=\"heading-anchor\" href=\"#testing-the-detailview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Oibríonn an méid atá againn go maith; ach, cé nach bhfuil ceisteanna amach anseo le feiceáil san <em>innéacs</em>, is féidir le húsáideoirí iad a bhaint amach má tá an URL ceart ar eolas acu nó má dhéanann siad buille faoi thuairim. Mar sin caithfimid srian den chineál céanna a chur le <code class=\"docutils literal notranslate\"><span class=\"pre\">DetailView</span></code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/views.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">DetailView</span><span class=\"p\">(</span><span class=\"n\">generic</span><span class=\"o\">.</span><span class=\"n\">DetailView</span><span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_queryset</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Excludes any questions that aren&#39;t published yet.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">pub_date__lte</span><span class=\"o\">=</span><span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">())</span>\n</code></pre></figure>\n<p>Ba cheart dúinn roinnt tástálacha a chur leis ansin, lena chinntiú gur féidir <code class=\"docutils literal notranslate\"><span class=\"pre\">Ceist</span></code> a bhfuil a <code class=\"docutils literal notranslate\"><span class=\"pre\">dáta_</span> <span class=\"pre\">tábhairne</span></code> san am a chuaigh thart a thaispeáint, agus nach bhfuil ceann le <code class=\"docutils literal notranslate\"><span class=\"pre\">dáta_</span> <span class=\"pre\">tábhairne</span></code> amach anseo:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">polls/tests.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">QuestionDetailViewTests</span><span class=\"p\">(</span><span class=\"n\">TestCase</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_future_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        The detail view of a question with a pub_date in the future</span>\n<span class=\"sd\">        returns a 404 not found.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">future_question</span> <span class=\"o\">=</span> <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Future question.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">5</span><span class=\"p\">)</span>\n        <span class=\"n\">url</span> <span class=\"o\">=</span> <span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:detail&quot;</span><span class=\"p\">,</span> <span class=\"n\">args</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">future_question</span><span class=\"o\">.</span><span class=\"n\">id</span><span class=\"p\">,))</span>\n        <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">url</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">status_code</span><span class=\"p\">,</span> <span class=\"mi\">404</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_past_question</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        The detail view of a question with a pub_date in the past</span>\n<span class=\"sd\">        displays the question&#39;s text.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">past_question</span> <span class=\"o\">=</span> <span class=\"n\">create_question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;Past Question.&quot;</span><span class=\"p\">,</span> <span class=\"n\">days</span><span class=\"o\">=-</span><span class=\"mi\">5</span><span class=\"p\">)</span>\n        <span class=\"n\">url</span> <span class=\"o\">=</span> <span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s2\">&quot;polls:detail&quot;</span><span class=\"p\">,</span> <span class=\"n\">args</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">past_question</span><span class=\"o\">.</span><span class=\"n\">id</span><span class=\"p\">,))</span>\n        <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">client</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">url</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertContains</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"p\">,</span> <span class=\"n\">past_question</span><span class=\"o\">.</span><span class=\"n\">question_text</span><span class=\"p\">)</span>\n</code></pre></figure>\n</section>\n<section id=\"ideas-for-more-tests\">\n<h3>Smaointe le haghaidh tástálacha níos mó<a class=\"heading-anchor\" href=\"#ideas-for-more-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Ba chóir dúinn modh cosúil le <code class=\"docutils literal notranslate\"><span class=\"pre\">get_queryset</span></code> a chur le <code class=\"docutils literal notranslate\"><span class=\"pre\">ResultsView</span></code> agus rang tástála nua a chruthú don amharc sin. Beidh sé an-chosúil leis an rud atá díreach cruthaithe againn; i ndáiríre beidh go leor athrá.</p>\n<p>We could also improve our application in other ways, adding tests along the\nway. For example, it's pointless that a <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> with no related <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code>\ncan be published on the site. So, our views could check for this, and exclude\nsuch <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> objects. Our tests would create a <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> without a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code>, and then test that it's not published, as well as create a similar\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> <em>with</em> at least one <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code>, and test that it <em>is</em> published.</p>\n<p>Perhaps logged-in admin users should be allowed to see unpublished <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>\nentries, but not ordinary visitors. Again: whatever needs to be added to\nthe software to accomplish this should be accompanied by a test, whether you\nwrite the test first and then make the code pass the test, or work out the\nlogic in your code first and then write a test to prove it.</p>\n<p>Ag pointe áirithe caithfidh tú breathnú ar do thástálacha agus smaoineamh an bhfuil do chód thíos le bloat tástála, rud a thugann dúinn:</p>\n</section>\n</section>\n<section id=\"when-testing-more-is-better\">\n<h2>Nuair a dhéantar tástáil, tá níos mó níos fearr<a class=\"heading-anchor\" href=\"#when-testing-more-is-better\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>D’fhéadfadh sé a bheith cosúil go bhfuil ár gcuid tástálacha ag éirí as smacht. Ag an ráta seo ní fada go mbeidh níos mó cód inár dtástálacha ná mar atá inár bhfeidhmchlár, agus ní bheidh an t-athrá athrá, i gcomparáid le beacht galánta an chuid eile dár gcód.</p>\n<p><strong>Is cuma</strong>. Lig dóibh fás. Don chuid is mó, is féidir leat triail a scríobh uair amháin agus ansin déan dearmad faoi. Leanfaidh sé ag feidhmiú a fheidhm úsáideach de réir mar a leanann tú ar aghaidh ag forbairt do chláir.</p>\n<p>Sometimes tests will need to be updated. Suppose that we amend our views so that\nonly <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> entries with associated <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code> instances are published.\nIn that case, many of our existing tests will fail - <em>telling us exactly which\ntests need to be amended to bring them up to date</em>, so to that extent tests\nhelp look after themselves.</p>\n<p>Ar an gceann is measa, agus tú ag leanúint ar aghaidh ag forbairt, b’fhéidir go bhfaighidh tú amach go bhfuil roinnt tástálacha agat nach bhfuil gá leo anois. Fiú ní fadhb í sin; is rud <em>mhaith</em> é iomarcaíocht a thástáil.</p>\n<p>Chomh fada agus a bheidh do thástálacha socraithe go ciallmhar, ní bheidh siad inbhainistithe. I measc na rialacha maithe tá:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">TestClass</span></code> ar leith do gach samhail nó radharc</p></li>\n<li><p>modh tástála ar leith do gach sraith coinníollacha is mian leat a thástáil</p></li>\n<li><p>ainmneacha modhanna tástála a chuireann síos ar a bhfeidhm</p></li>\n</ul>\n</section>\n<section id=\"further-testing\">\n<h2>Tástáil bhreise<a class=\"heading-anchor\" href=\"#further-testing\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Ní thugann an rang teagaisc seo isteach ach cuid de bhunghnéithe na tástála. Tá i bhfad níos mó is féidir leat a dhéanamh, agus roinnt uirlisí an-úsáideach ar fáil duit chun roinnt rudaí an-chliste a bhaint amach.</p>\n<p>Mar shampla, cé gur chlúdaigh ár dtástálacha anseo cuid de loighic inmheánach múnla agus an chaoi a bhfoilsíonn ár dtuairimí faisnéis, is féidir leat creat &quot;i-bhrabhsálaí&quot; a úsáid mar <a class=\"reference external\" href=\"https://www.selenium.dev/\">Selenium</a> chun an bealach a rindreálann do HTML i mbrabhsálaí a thástáil. Ligeann na huirlisí seo duit ní hamháin iompar do chód Django a sheiceáil, ach freisin, mar shampla, do JavaScript. Is fiú go mór na tástálacha a fheiceáil ag seoladh brabhsálaí, agus tosú ag idirghníomhú le do shuíomh, amhail is dá mba duine daonna é! Áirítear le Django <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">:rang:`~django.test.LiveServerTestCase`</span></a> chun comhtháthú le huirlisí cosúil le Seiléiniam a éascú.</p>\n<p>Má tá feidhmchlár casta agat, b'fhéidir gur mhaith leat tástálacha a rith go huathoibríoch le gach gealltanas chun críocha <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Continuous_integration\">comhtháthú leanúnach</a>, ionas go mbeidh rialú cáilíochta ann féin - go páirteach ar a laghad - uathoibrithe.</p>\n<p>Bealach maith le codanna de d’iarratas nár tástáladh a fheiceáil ná clúdach an chóid a sheiceáil. Cuidíonn sé seo freisin le cód leochaileach nó fiú marbh a aithint. Mura bhfuil tú in ann píosa cód a thástáil, is gnách go gciallaíonn sé gur cheart an cód a athmhacrú nó a bhaint. Cabhróidh clúdach le cód marbh a aithint. Féach <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">:tag:`topics-testing-code-coverage`</span></a> le haghaidh sonraí.</p>\n<p><a class=\"reference internal\" href=\"/ga/5.2/topics/testing/\"><span class=\"doc\">Testing in Django</span></a> tá faisnéis chuimsitheach faoi thástáil.</p>\n</section>\n<section id=\"what-s-next\">\n<h2>Cad atá romhainn?<a class=\"heading-anchor\" href=\"#what-s-next\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Chun sonraí iomlána na tástála a fháil, féach <a class=\"reference internal\" href=\"/ga/5.2/topics/testing/\"><span class=\"doc\">Testing in Django</span></a>.</p>\n<p>Agus tú ar do chompord le tuairimí Django a thástáil, léigh <a class=\"reference internal\" href=\"/ga/5.2/intro/tutorial06/\"><span class=\"doc\">cuid 6 den rang teagaisc seo</span></a> chun foghlaim faoi bhainistiú comhad statach.</p>\n</section>","rootId":"writing-your-first-django-app-part-5","toc":[{"title":"Tástáil uathoibrithe a thabhairt isteach","anchor":"introducing-automated-testing","children":[{"title":"Cad is tástálacha uathoibrithe ann?","anchor":"what-are-automated-tests","children":[]},{"title":"Cén fáth ar gá duit tástálacha a chruthú","anchor":"why-you-need-to-create-tests","children":[{"title":"Sábhálfaidh tástálacha am duit","anchor":"tests-will-save-you-time","children":[]},{"title":"Ní hamháin go n-aithníonn tástálacha fadhbanna, cuireann siad cosc ​​orthu","anchor":"tests-don-t-just-identify-problems-they-prevent-them","children":[]},{"title":"Déanann tástálacha do chód níos tarraingtí","anchor":"tests-make-your-code-more-attractive","children":[]},{"title":"Cuidíonn tástálacha le foirne oibriú le chéile","anchor":"tests-help-teams-work-together","children":[]}]}]},{"title":"Straitéisí bunúsacha tástála","anchor":"basic-testing-strategies","children":[]},{"title":"Ag scríobh ár gcéad triail","anchor":"writing-our-first-test","children":[{"title":"Aithnímid fabht","anchor":"we-identify-a-bug","children":[]},{"title":"Cruthaigh tástáil chun an fabht a nochtadh","anchor":"create-a-test-to-expose-the-bug","children":[]},{"title":"Rith tástálacha","anchor":"running-tests","children":[]},{"title":"An fabht a shocrú","anchor":"fixing-the-bug","children":[]},{"title":"Tástálacha níos cuimsithí","anchor":"more-comprehensive-tests","children":[]}]},{"title":"Tástáil radharc","anchor":"test-a-view","children":[{"title":"Tástáil le haghaidh radharc","anchor":"a-test-for-a-view","children":[]},{"title":"Django cliant tástála","anchor":"the-django-test-client","children":[]},{"title":"Ár dtuairim a fheabhsú","anchor":"improving-our-view","children":[]},{"title":"Ár radharc nua a thástáil","anchor":"testing-our-new-view","children":[]},{"title":"Ag tástáil an DetailView","anchor":"testing-the-detailview","children":[]},{"title":"Smaointe le haghaidh tástálacha níos mó","anchor":"ideas-for-more-tests","children":[]}]},{"title":"Nuair a dhéantar tástáil, tá níos mó níos fearr","anchor":"when-testing-more-is-better","children":[]},{"title":"Tástáil bhreise","anchor":"further-testing","children":[]},{"title":"Cad atá romhainn?","anchor":"what-s-next","children":[]}],"breadcrumbs":[{"docname":"intro/index","title":"Ag tosnú","url":"/ga/5.2/intro/"}],"prev":{"docname":"intro/tutorial04","title":"Do chéad aip Django a scríobh, cuid 4","url":"/ga/5.2/intro/tutorial04/"},"next":{"docname":"intro/tutorial06","title":"Do chéad aip Django a scríobh, cuid 6","url":"/ga/5.2/intro/tutorial06/"},"formats":{"html":"/ga/5.2/intro/tutorial05/","markdown":"/ga/5.2/intro/tutorial05.md","json":"/ga/5.2/intro/tutorial05.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/intro/tutorial05.txt","official":"https://docs.djangoproject.com/ga/5.2/intro/tutorial05/","inVersions":["6.1","6.0","5.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}