1

I'm working through 100 days of code. One of the projects is to implement pytest over a project of choice. I've chosen a previous project that takes a defined dictionary of car makes and models and returns output to various questions. I've written a unit test for two of these functions and they are failing.

Running the code from the console for the function get_all_jeeps() will return:

Grand Cherokee, Cherokee, Trailhawk, Trackhawk

If I run pytest with the following code:

def test_get_all_jeeps():
    expected = 'Grand Cherokee, Cherokee, Trailhawk, Trackhawk'
    actual = get_all_jeeps()
    assert type(actual) == str
    assert actual == expected

It fails because pytests output looks like it is being sorted. Why would it do that?

E       AssertionError: assert 'Cherokee, Gr...wk, Trailhawk' == 'Grand Cherokee...wk, Trackhawk'
E         - Cherokee, Grand Cherokee, Trackhawk, Trailhawk
E         + Grand Cherokee, Cherokee, Trailhawk, Trackhawk

The other test is giving a different output to when it is run from the console. Output from the console for the function get_first_model_each_manufacturer() is:

['Falcon', 'Commodore', 'Maxima', 'Civic', 'Grand Cherokee']

Except it fails pytest:

    def test_get_first_model_each_manufacturer():
        expected = ['Falcon', 'Commodore', 'Maxima', 'Civic', 'Grand Cherokee']
        actual = get_first_model_each_manufacturer()
        assert type(actual) == list
>       assert actual == expected
E       AssertionError: assert ['Fairlane', ...', 'Cherokee'] == ['Falcon', 'Co...and Cherokee']
E         on index 0 diff: 'Fairlane' != 'Falcon'
E         Use -v to get the full diff

How does the item 'Fairlane' get there? What is pytest doing differently?

Repo here https://github.com/cadamei/100daysofcode/tree/master/days/10-12-pytest

All the functions use this dictionary as the data:

cars = {
    'Ford': ['Falcon', 'Focus', 'Festiva', 'Fairlane'],
    'Holden': ['Commodore', 'Captiva', 'Barina', 'Trailblazer'],
    'Nissan': ['Maxima', 'Pulsar', '350Z', 'Navara'],
    'Honda': ['Civic', 'Accord', 'Odyssey', 'Jazz'],
    'Jeep': ['Grand Cherokee', 'Cherokee', 'Trailhawk', 'Trackhawk']
cadamei
  • 13
  • 5
  • Are you are using a database? Some databases give no guarantee of consistent ordering between queries (notably Postgresql). Anyway if order is not relevant you can fix the test using `assert set(actual) == set(expected)`. – Paulo Scardine Jun 15 '19 at 11:15
  • I've added the dict it uses. Does that help? – cadamei Jun 15 '19 at 11:21

1 Answers1

0

Your cars.py script is modifying the list because it is running print(sort_car_models()) when imported, remove those lines or put them in if __name__ == '__main__':

To learn more about that, take a look at What does if __name__ == "__main__": do?

Stradivari
  • 2,626
  • 1
  • 9
  • 21
  • Anorther "solution" would be to use `sorted` instead of modifying the list with `sort`. https://stackoverflow.com/questions/22442378/what-is-the-difference-between-sortedlist-vs-list-sort – Stradivari Jun 15 '19 at 11:37