CS2613/journal/_src/posts/2022-10-31-lab-14.md
2022-11-01 23:49:37 -03:00

1.5 KiB

Title: Lab Fourteen
Date: 2022-10-31T08:30:00
Tags: cs2613, lab, python, pytest, exceptions, modules

In this lab I learned about python, pytest, python exceptions and modules.

Pytest

Pytest is a python testing framework. It can show you code coverage with line counts. This is pretty similar to nyc from javascript. You run tests by just running pytest or pytest -cov for code coverage. Tests are defined by in a file having a function with an assert statement. Pytest will then scan your files for assert statements and run those functions to run tests. Testing in python is pretty simple, like with javascript. Racket testing is a little more abstract and confusing in my opinion.

Modules

In python functions are exported by default, in contrast to javascript. You must guard your main method with the if __name__ =='__main__' text so it does not run when the module is imported. You can make tests to ensure you have documentation for your functions, by checking for the imports __doc__ variable.

Indentation

In python, blocks are defined by indentation (also known as whitespace). This sets the scope for a given statement. It can be a little confusing to tell if an if statement is in a for loop or not, but linters help.

Exceptions

Python can raise errors when things go wrong, for example diving by zero. You can catch these exceptions and handle them if you desire.

FizzBuzz missing case

You can fix the missing case just by adding another if statement above the other if statement for the fizz case.