0

I thought Django was not supposed to print any output in the tests....I have some print statements that help me with data entry, and I have written TestCases for them, but when the tests run they print all the output to the terminal which is annoying.

Is there a way to stop django from printing them while testing? Why isn't it doing this already?

Model Method:

def wiktionary_lookup(self, wiktionary_prefix, driver):

    driver.get("http://%s.wiktionary.org/wiki/%s" % (wiktionary_prefix, self.name))
    definitions = driver.find_elements_by_xpath("//h3/following-sibling::ol/li")

    count = 0
    defs_list = []

    print "\tWIKTIONARY DEFINITIONS:\n"
    for i in definitions:
        i = i.text.split('\n')
        for j in i:
            #Takes out an annoying "[quotations]" in the end of the string, soemtimes. 
            j = re.sub(u'\u2003\[quotations \u25bc\]', '', j)
            print "\t%d. %s" % (count, j)
            defs_list.append(j)
            count += 1
    print "\n"

    return defs_list

Test:

def test_wiktionary_lookup(self):
    language = Language.objects.create(name='eng', full_name='English')
    word = Word.objects.create(language=language, name='testword')
    driver = webdriver.Chrome()
    output = word.wiktionary_lookup('en', driver)
    self.assertTrue(len(output) == 0)
    word = Word.objects.create(language=language, name='table')
    output = word.wiktionary_lookup('en', driver)
    self.assertTrue(len(output) > 0)
3
  • Please post your code. Commented Jul 19, 2016 at 9:48
  • 4
    That's normal; you shouldn't use print statements in your code. You could use the logger if that's useful. Commented Jul 19, 2016 at 9:53
  • @ZachZundel code psoted Commented Jul 19, 2016 at 9:57

1 Answer 1

2

Your wiktionary_lookup function is doing the printing. Your test cases aren't going to alter the functionality of your wiktionary_lookup code -- in fact, if they did, they wouldn't be very good test cases! (You'd be testing your altered code and not the real stuff.) If you don't want it to print, you can use a logger or something, like François mentioned.

Here is a link to the Django documentation on logging: https://docs.djangoproject.com/en/1.9/topics/logging/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.