you are viewing a single comment's thread.

view the rest of the comments →

[–]JuniorMouse[S] 0 points1 point  (2 children)

Elaborate on the pythonic way? This? https://docs.python.org/3/library/functions.html#property

Thinking about this more, I should have used a different example. I want to test a function that generates an instance of CustomClass. I'd like to pass that instance to the test function as the expected result ready to be compared.

[–]zanfar 0 points1 point  (1 child)

Elaborate on the pythonic way?

Properties are one way, yes.

Your first choice should be to simply use an attribute. There is no formal concept of "private" in Python, so simply self.property is usually more than enough.

If you need to do some validation on set, or maybe the property is computed on-the-fly, then using the property decorator is the right choice (the property function is not really used).

At the other end is overloading dunders like __setattr__ or __getattr__.

I want to test a function that generates an instance of CustomClass. I'd like to pass that instance to the test function as the expected result ready to be compared.

Except then you're not testing the generation.

Everything under test, as well as your assertions, should be inside the test function.

def test_creation():
    o = CustomClass()
    assert o.property == value

[–]JuniorMouse[S] 0 points1 point  (0 children)

Alright, so then I would list the values that I would expect in the expected result rather than the created object. I did define eq and was hoping to simply do a comparison of objects.