FAQ: Unit Testing - Review

This community-built FAQ covers the “Review” exercise from the lesson “Unit Testing”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Python 3

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I am having difficulties to create a unit test for the second function-request_flight_attendant() in the monitor.py
How can I make a test that checks whether the requesting the flight attendant action creates the intended warning which is
warnings.warn('A customer has requested attention!', CustomerRequestWarning)

Hi,
I’m sure you’ve already found an answer, but maybe it would be useful for someone else:

class TestMonitor(unittest.TestCase):

  def test_request_flight_attendant(self):

    self.assertWarns(monitor.CustomerRequestWarning, monitor.request_flight_attendant)

Br,
Milena

4 Likes

Hi there,
Please can anyone nudge me in the right direction, I was expecting this unit test to fail on the <1 and ‘string’ iterations of the test loop, what am I missing? Thanks in advance :slight_smile:

import unittest import warnings class CustomerRequestWarning(Warning): pass def calculate_remaining_time(km): ''' This function estimates the remaining flight time when given the kilometers that remain for this flight. The function assumes an average speed of 15 km per minute and rounds down when calculating minutes. It returns a tuple of hours and then minutes remaining. ''' total_minutes = int(km / 15) minutes = total_minutes % 60 hours = int(total_minutes / 60) return (hours, minutes) def request_flight_attendant(): ''' This function raises a CustomerRequestWarning to let the flight attendants know that they are requested. ''' warnings.warn('A customer has requested attention!', CustomerRequestWarning) class SmallWorldTest(unittest.TestCase): def test_calculate_remaining_time(self): for num in [15, 900, 2730, 5, 0, -1, 'hey']: with self.subTest(): if type(num) is not int: expected_result = 'Please enter a valid distance' elif num < 1: expected_result = 'You have reached your destination' else: total_minutes = int(num / 15) minutes = total_minutes % 60 hours = int(total_minutes / 60) expected_result = (hours, minutes) message = 'Expected calculate_remaining_time(' + str(num) + ') to return ' + str(expected_result) self.assertEqual(calculate_remaining_time(num), expected_result, message) unittest.main()

Ah, didn’t have assert statements in the right places! :slight_smile:

Hello,
I’m having some trouble trying to understand the solution that’s under the “help options”.
Can someone help me out please?

def test_remaining_time(self):
expected_values = {
15: (0, 1),
500: (0, 33),
1000: (1, 6)
}
for k, v in expected_values.items():
with self.subTest(i=k):
self.assertEqual(monitor.calculate_remaining_time(k), v)