20.9. Group Work on Unit Tests

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.

Note

If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.

Content Learning Objectives

After completing this activity, students should be able to:

It is important to learn how to write good tests, this is especially true if other people will use your code, but it is also true when it is just for your own use.

20.9.1. Assert methods

There are many assert methods that you can use in your tests. See https://docs.python.org/3/library/unittest.html for the documentation. You will mostly use assertEqual, but here are a few others that are very useful.

Method

What the Method Tests

assertEqual(a,b)

a == b

assertTrue(x)

x == True

assertGreater(a,b)

a > b

assertGreaterEqual(a,b)

a >= b

assertIn(a,b)

a in b

assertIsInstance

isinstance(a, b)

assertAlmostEquals(a,b,p)

round(a-b, p) == 0, the default for p (num places after decimal point) is 7

Note

All the assert methods also take an optional message (string) to display as the last parameter.

20.9.2. Writing Unit Tests

When you create tests you want to test the usual things as well as edge cases (unusual things). For example, when working with lists what is the result when the list is empty? When working with numbers be sure to test positive and negative numbers.

Read the description of the function below. How many test cases do you need to test this function?

Write a function is_descending(nums) that returns True if the numbers in the list nums are sorted in descending order and False otherwise. If the list nums has less than two numbers in it return True. For example, is_descending([3, 2, 1]) should return True, is_descending([1]) should also return True, and is_descending([1,2,3]) should return False.

Add a test to the testOne function above to test when nums is the empty list. Add tests to check what happens when all numbers are descending except the first, middle, or last. Add tests that check that it works for unusual values such as negative numbers or zero.

Read the description of the function below. How many test cases do you need to test this function?

Write a function temp_cat(value) that returns "low" if value is < 97, normal if value is >= 97 and <= 99, and high if value is > 99. Add more test cases to check the three possible return values ("low", "normal", and "high").

Did you check when value equals 97 and is greater than 97? Did you check when value was equal to 99? What happens if value is a floating point number like 98.7 or 102.4?

Use assertEqual if the values you are comparing are integers or strings. Use assertAlmostEqual if the values are floating point numbers (especially if they are calculated by the computer). This method takes the two items to compare, the number of decimal places to compare (the default is 7), and a string describing the test.

Write a total method in the Order class to return the total of all of the prices for the items in the order. Then add a test to testTotal to test the total of o2.

Note

Remember that the setUp method is called before every method of the class myTests and that the methods are not called in the order they are written.

It is easy to test a function or method that returns a value, but how do you test when no value is returned? For example, the set_price method below doesn’t return anything, but it should change the price. You can test that the price changed from the original value as shown below.

Write a set_name method that changes the current object’s name and then add a new method test_set_name to test set_name.

Note

To test a method or function in a test method be sure to call the method or function.

Testing with inheritance. If you have asked someone to create a class that inherits from another class you might want to check that they specified the inheritance correctly. You can use assertIsInstance to check if an object is an instance of a class.

Create a subclass of the Animal class named Duck that overrides the inherited noise method to return "Quack". Also create a test to check that the noise method in Duck returns the correct string.

If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.

The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.

You have attempted of activities on this page