Starting from:

$30

EECS1015-Lab 7 Writing Testing Code For Use With PyTest Solved

TASK 1 – Checking Pre-condition for empty list [modify getMinMax() ]  

Modify the function getMinMax(List[int], str) to include an assert statement to check the pre-condition that the list is not empty.   There is already an assert checking if the str is either "MIN" or "MAX". 

TASK 2 – Test functions for our code. [write five (5) Pytest functions] 

Modify the starting code to include the following five (5) test functions for use with Pytest. 

These functions should appear at the end of the current code.   Please name your functions exactly as shown below. 

(1) def test_getMinMaxCase1(): 

This function will test a standard use case for our minmaxList. 

(a)  Create a list with two items that are different. 

(b)  Call initializeList() with (a). 

(c)  Use getMinMax() to get the mimimum item. Use an assert statement to check if this is correct. 

   Error message should be "Min should be x", where x is the minimum item in the list specified in (a). 

(d)  Use getMinMax() to get the maximum item. Use an assert statement to check if this is correct. 

   Error message should be "Max should be x", where x is the maximum item in the list specified in (a). 

(2) def test_getMinMaxCase2(): 

This function will test an edge case where the list only has a single item. 

(a)  Create a list with only 1 item, let's call this item y. 

(b)  Call initializeList()  with (a). 

(c)  Use getMinMax() to get the mimimum item (which is y). Use an assert statement to check if this is correct. 

  Error message should be "Min should be y", where y is the single item in your list in (a). 

(d)  Use insertItem() to insert the same item y back into the list in (a). 

(e)  Use getMinMax() to get the maximum item (which is y). Use an assert statement to check if this is correct. 

  Error message should be "Max should be y", where y is the maximum item in the list specified in (a). 

(3) def test_getMinMaxCase3(): 

This function will test an edge case where the list starts out empty. 

(a)  Create an empty list. 

(b)  Call initializeList()  with (a). 

(c)  Insert an item x into (a) using insertItem(). 

(d)  Insert an item y into (a) using insertItem().  Item y should be larger than x. 

(e)  Use getMinMax() to get the mimimum item. Use an assert statement to check if this is correct. 

  Error message should be "Min should be x", where x is the minimum item inserted into (a). 

(f)   Use getMinMax() to get the maximum item. Use an assert statement to check if this is correct.   Error message should be "Max should be y", where y is the maximum item inserted into (a). 

(4) def test_getMinMaxRequestError() 

This function will test to see if getMinMax() properly causes an assertion error when the string argument is not correct. 

(a)  Create a list with 3 items. 

(b)  Call initializeList()  with (a). 

(c)  Call getMinMax() with a, but using "MID" instead of "MIN" or "MAX".  This will cause getMinMax() to raise an AssertionError. 

(d)  Check if the AssertionError was raised.  Assert on this condition, if the condition was not rasie, your error should be: 

   "Should raise AssertionError!" 

Continues on next page.                                                             

 

(5) def test_getMinMaxEmptyError(): 

This function will test to see if getMinMax() properly causes an assertion error when the list is empty. 

(a)  Create an empty list. 

(b)  Call initializeList()  with (a). 

(c)  Call getMinMax().  If you did Task 1 correctly, this will cause getMinMax() to raise an AssertionError. 

(d)  Check if the AssertionError was raised.  Assert on this condition, if the condition was not rasie, your error should be: 

   "Should raise AssertionError!" 

VERIFYING YOU TEST FUNCTIONS WITH PYTEST 

Remember to first install Pytest using the pip command as described in the notes.  Now, verify that your test functions work by using "pytest lab7.py" from PyCharm's terminal.   

To the best of our knowledge, the code provided to you does not have any bugs, so the 5 test should all pass if written correctly. 

The expected output would look as follows. 

(venv) C:\Users\mbrown\PycharmProjects\pythonProject1pytest Lab7.py ==================== test session starts ====================  platform win32 -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 rootdir: C:\Users\mbrown\PycharmProjects\pythonProject1 

collected 5 items                                                                                        

 

Lab7.py .....                                                                                            

[100%] 

 

==================== 5 passed in 0.04s ==================== 

 

NOTE: You can force a failure of your test cases by commenting out one of the assert statements in the function getMinMax(). 

More products