r/learnpython • • Sep 15 '23

How should I be testing the __init__ of my class?

I'm building a wrapper for an API and I'm in the process of building my unit tests.

One thing I'm unclear on is how I should be approaching the testing of my class's __init__ method?

Given that the attribute's of my class are derived from a JSON API response, should I my class's attribute types?

3 Upvotes

9 comments sorted by

7

u/zanfar Sep 15 '23

You should test the initialization of the class under all the possible circumstances it's supposed to work.

  • What is the minimal response returned by the API?

  • What is the most verbose response returned by the API?

  • What are all the possible variations of that response?

my __init__ method calls a private method that calls the API.

I would probably reverse that and break it up. I don't think it's the model's responsibility to call an API. The API code should make the request, and then instantiate an object to return.

Initialization should do as little heavy lifting as possible, IMO.

Use functions with the smallest responsibility possible and chain them, rather than building highly coupled monoliths. I generally have some base "make request" function, which is expanded on by the specific function for the API call and returns some known schema. Then the object factory calls the API method, massages that schema however necessary, then passes the specific info to the object initialization and returns the object.

It's likely you're having trouble understanding how to test because the functions themselves do too much, so there are few boundaries to enforce.

1

u/[deleted] Sep 15 '23

Thank you

1

u/JambaJuiceIsAverage #1 pandas hater Sep 15 '23

To say a similar thing in different words, I learned early on to try to clearly differentiate between data objects (which often are dictionaries or look a lot like them) and "calling" objects (usually functions) which return the data to instantiate your data objects.

Obviously that's a big oversimplification but I start most of my projects by trying to establish what is basically a dictionary and what is basically a function, then piece it together from there. Works well for me.

5

u/[deleted] Sep 15 '23

Have your test contain a model JSON string, and make sure it works with your __init__ method. If you really want to get rigorous about it, use Hypothesis to generate random JSON's that fit the schema and make sure they don't cause errors when you instantiate your class from them.

0

u/[deleted] Sep 15 '23

Hmm the only thing is my `__init__` method calls a private method that calls the API.

2

u/[deleted] Sep 15 '23

Well, I generally recommend spending your testing effort on stuff that can break.

1

u/[deleted] Sep 15 '23

Sorry, what I meant was, should I use a model JSON string if the __init__ involves an API call?

3

u/[deleted] Sep 15 '23

No, your tests shouldn't make calls to external API's.

2

u/[deleted] Sep 15 '23

Ah so that’s the purpose of providing a mock JSON string?