Joff
Joff

Reputation: 12197

Why are Django tests printing output?

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)

Upvotes: 0

Views: 812

Answers (1)

Zach Zundel
Zach Zundel

Reputation: 415

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/

Upvotes: 2

Related Questions