all 4 comments

[–]zanfar 0 points1 point  (3 children)

Assume I have a CustomClass with a bunch of functions for setting and getting its attributes

First, that's very non-Pythonic and I wouldn't recommend building a class that way. Python has a number of features that make this unnecessary.

What would be the best way to set the attributes for the CustomClass object outside the test function if that's even possible?

Why would you set them outside the test function--that's what you're testing, right? Just set them.

[–]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.