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']