Code Style(Elegance) and Unit Tests
Code Style/Elegance Introduction
Automated tools can help us to catch low-level formatting issues like incorrect spacing. They can’t really help us to write clean code: code that is concise and easy to understand. The goal of today’s activity is to gain practice cleaning up style issues in poorly-written coded.
Hat Store
Download the following two files:
The first file contains some classes that are being developed for an inventory management system for a hat store. This file is functionally correct, but it has many style issues. Work through this file to correcting many of these issues as possible. You can use the unit tests to ensure that your changes aren’t introducing errors with the functionality of the code.
More Unit Testing
Introduction
One thing we’ve learned through decades of software engineering is that developing correct software is difficult! The best way to ensure correctness is to test thoroughly while software is being developed. Unit testing is a framework that automates testing by providing a standard format for tests and an easy way to execute them.
Today we will practice writing unit tests for the ParkingLot
class from HW9.
Writing Tests
Download the following four files:
Note that .pyc
files are compiled byte-code versions of Python files. They can be executed and imported like .py
files, but are not human-readable. These .pyc
files are tied to a particular version of Python. They should work on the lab machines, but they may not work on your personal machine.
The file test_parking.py
already contains a few reasonable tests for the best_fit
method. Try adding some tests for park
. Ideally, you tests should all pass when executed against parking.pyc
. We’ve intentionally introduced at least one error into parking_broken.pyc
. You can run your tests against this file by changing this line:
to this: