Sunday, July 10, 2016

Re: Error in testing script

On 07/10/2016 01:57 PM, Gary Roach wrote:
> Hi all;
>
> OS Debian Linux KDE desktop
> Django 1.9
> Python 3.5
>
> Working with tutorial
> https://docs.djangoproject.com/en/1.9/intro/tutorial05/
> Writing polls/tests.py script
>
> I am consistently getting an error:
> NameError: name 'create_question' is not defined.
>
> The first section of tests.py to throw this error is:
> def test_index_view_with_two_past_questions(self):
> """
> The questions index page may display multiple questions.
> """
> create_question(question_text="Past question 1.", days=-30)
> create_question(question_text="Past question 2.", days=-5)
> response = self.client.get(reverse('polls:index'))
> self.assertQuerysetEqual(
> response.context['latest_question_list'],
> ['<Question: Past question 2.>', '<Question: Past question
> 1.>']
> )
>
> The code example is in class QuestionMethodTests(TestCase): but is
> flagged 8 times by the editor throughout the code in test.py. The same
> error pops at run time.
>
> The code seems to be identical to the code in the tutorial.
>
> All of the errors occur on code lines like:
> create_question(question_text=" .......
>
> All help will be appreciated.
>
> Gary R.
>
I missed something. The create_question call is a def defined earlier in
the class.

def create_question(question_text, days):
"""
Creates a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions
published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + datetime.timedelta(days=days)
return Question.objects.create(question_text=question_text,
pub_date=time)

this code throws an error : Method 'create_question - polls.tests'
should have self as first parameter.

Here again, the code seems to be identical to that in the tutorial.
Adding self to the def clears this problem but does not clear the rest
of the errors. The def doesn't seem to be working.

Gary R

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/16e9912e-6669-939e-87d9-7f5588306923%40verizon.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment