reading-notes

reading notes for code fellows


Project maintained by dLeigh01 Hosted on GitHub Pages — Theme by mattgraham

Classes, Recursion, Fixtures and Coverage

These topics are important because being able to use classes makes code a lot more dry and I would assume fixtures will as well in terms of testing. Coverage is important to make sure you’ve found all the possible ways that something could mess up your code.

Classes and Objects

Objects are a grouping of variables and functions, and Classes are the template to create them. A basic class would be written as:

 class MyClass:
    var = 'variable'

    def function(self):
      print('hello')

and you would use myobject = MyClass() to assign it. You use the normal dot notation to access values within the class from its new object container. Multiple objects can have the same class assigned and can independantly change the data within their copy.

If you are assigning variables within a class, you would use def __init__(self, variable)

Pytest Fixtures and Coverage

If you’re running a multitude of tests and have objects that you want to have available to all of them, you’ll want to create something known as a fixture, which can be defined using the pytest.fixture decorator and a function definition. If you want to use said fixture in one of your tests, you can then include the name in the parameters of the necessary test. You can also change how often its run by doing things like passing in a scope on the decorator when you create the fixture; if you use scope='module' it will only execute once, but will be available to all of your tests.

If you are concerned there may be paths that will raise exceptions within your code that the tests you’ve written haven’t caught, you can download pytest-cov and invoke it with --cov=directory and then give it something like coverage html to make it readable. After this, you will have a file that gives you a coverage report showing where your program lacks coverage.

Things I’d Like to Know More About

I want to know a bit more about how coverage works and at exactly what is going on with the fixtures, I’m not 100% on them.

Discussion

Classes and objects once again seem like they’re going to be similar to how they were in javascript, so I’m feeling pretty good about those, alternatively, I haven’t encountered anything quite like fixtures and coverage before - mostly because I haven’t been needing to write tests until now I assume - but fixture seems like something along the lines of a global variable, but for tests, and coverage is a sort of grade assigned to the tests you’ve written.

[< table of contents]

[< home]