Note 20.9.1.
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.
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 |
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
.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.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"
).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?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.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.setUp
method is called before every method of the class myTests
and that the methods are not called in the order they are written.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.set_name
method that changes the current object's name and then add a new method test_set_name
to test set_name
.assertIsInstance
to check if an object is an instance of a class.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.https://cspogil.org/Home
https://docs.python.org/3/library/unittest.html