Unit Testing and Code Coverage Classwork: Work individually on the following tasks. Before You Start: - Command-Line Shell: Use "Bash" (Windows) or "Terminal" (Mac). - Text Editor: Any Editor is fine, you can use "Notepad++" on Windows; on Mac OS, use any editor except TextEdit. - Access to Linux/Unix Commands Sheet: Unix Commands PDF. Basic Unix Commands https://www.cs.utexas.edu/~fares/cs330ef25/CS%20373_files/tutorials/UnixCommands.pdf Tasks: 1. Create a Python File on your laptop: Name: "Avg.py" Content: def avg(marks): assert len(marks) != 0 return sum(marks) / len(marks) assert avg([2]) == 1 assert avg([1,2,3]) == 2 2. In your command-line shell, run the Python File: Command: $ python Avg.py Question 1: What output do you get after executing the command (briefly describe)? Hint: If all assertions pass, Python produces no output. If an assertion fails, you'll see an AssertionError. 3. Create a Unit Test File in the same folder as "Avg.py": Name: "AvgTest.py" Content: from unittest import main, TestCase from Avg import avg class MyUnitTests(TestCase): def test_avg(self): self.assertEqual(avg([1, 2, 3]), 2) if __name__ == "__main__": main() 4. Run the Unit Tests: Command: $ python AvgTest.py Question 2: Is the output the same as in Question 1? If not, what's different? How can you resolve the error? After resolving it, re-run the command and confirm the output. 5. Measure Test Coverage: 5.1 Run the coverage command. $ coverage run --branch AvgTest.py 5.2 Print Coverage Report: $ coverage report -m Question 3: Do you have 100% coverage? why? why not? 6. Create a Makefile to automate running and reporting coverage: In the same directory as Avg.py and AvgTest.py, create a file named Makefile with the following content: Note: Be careful: each action line in the Makefile must start with a tab, not spaces. Content: AvgTest.tmp: Avg.py AvgTest.py coverage run --branch AvgTest.py > AvgTest.tmp 2>&1 coverage report -m >> AvgTest.tmp cat AvgTest.tmp Question 4: Identify the target, dependencies, and actions in the Makefile. What does "2>&1" do? Hint: "2>&1" combines error messages with normal program output so both are saved together. (See the unittest tutorial PPT on the class webpage for details.) 7. Run make with the target AvgTest.tmp: $ make AvgTest.tmp Question 5: What output do you get after executing the command (briefly describe)?