Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unit test is not running

I am trying to define a test method. Currently I am not receiving any errors, but the test is not actually running. The test is trying to make sure that only the first word in a string that is in list_first_words is being returned.

import unittest

class TestSong(unittest.TestCase):
    def first_words_list(self):
        self.assertEqual(Song().firstwords(["hello world"]),["hello"])

if __name__ == "__main__":
    unittest.main()

Code that is being tested:

def firstwords(self,large_song_list):
        all_first_words = [] # Create an empty list
        for track in large_song_list:
            first_word = track.trackName.partition(' ')[0]
            all_first_words.append(first_word)
        return all_first_words
like image 868
Avery9115 Avatar asked May 09 '26 08:05

Avery9115


2 Answers

You need to rename the test method to test_first_words_list.

Tests are discovered by unittest only when they start with the word test. See "Organizing Test Code" in the documentation for more details.

like image 72
Raymond Hettinger Avatar answered May 11 '26 21:05

Raymond Hettinger


As described in the documentation:

A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.

So, you need to rename the method starts with test.

like image 36
pkuphy Avatar answered May 11 '26 20:05

pkuphy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!