Skip to content

Testing with Coverage

Test coverage is a measure of how much of your code is run by your tests. Using coverage helps you find areas of code that are not being tested.

To run pytest with coverage:

  1. Click "Testing" (the bottom "" icon) on the left sidebar.
  2. Click any of the the "" buttons:
    • Run Test
    • Debug Test
    • Run Test with Coverage

The "" button is also shown in the editor to the left of each test function. You can right-click that button to access the "Run with Coverage" option.

Complete the following exercises to practice running tests with coverage.

Part 1: Triangles

Download triangles.py and test_triangles.py from the Installing/Running pytest instructions. Notice the provided test functions in test_triangles are incomplete.

  1. Run coverage to see which lines of triangles.py need to be tested.

  2. Add more assert statements to each function in test_trianges.py.

  3. Continue steps 1 and 2 until you get 100% coverage.

NOTE: You won't get 100% coverage of the triangles.py module, because some of the lines run only when __name__ == "__main__". However, you can add the comment # pragma: no cover to exclude the __main__ block from coverage analysis:

if __name__ == "__main__":  # pragma: no cover
    t = (3, 4, 5)
    print(f"valid({t}):", valid(t))
    print(f"area({t}):", area(t))
    print(f"classify({t}):", classify(t))

Part 2: Weather

Suppose that JMU had the following severe weather cancellation policy: (here's the real one)

JMU (FAKE) SEVERE WEATHER POLICY

If any of the following conditions are met, campus will be closed:

  • More than 6.0 inches of rain are predicted within the next 24 hours.
  • Wind speeds greater than 70.0 mph are predicted within the next 24 hours.
  • Predicting more than 4.0 inches of rain and wind speeds above 45.0 mph.

If none of the above conditions are met, the University may still issue a warning and encourage individuals to avoid non-essential outdoor activities. A warning will be issued if any of the following conditions are met:

  • Predicted wind speeds are above 45.0 mph.
  • Predicted precipitation is above 4.0 inches.

The following Python modules have been developed as part of JMU's decision support systems:

The weather_advice function provides an implementation of the cancellation policy above. Your job is to write a test module to confirm that this function is implemented correctly.

Submission

When finished, upload only test_weather.py to Gradescope.