python – How to call an external function without sending the 'self'?

Question:

I'm using a class attribute to store the View I want to test in a Django application, like this:

# TESTS.PY
class OrderTests(TestCase, ShopTest):
    _VIEW = views.order

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        self._VIEW(request, **{'sku': order.sku})


# VIEWS.PY
def order(request, sku)
     ...

But during execution, as I call an attribute of my OrderTests object, Python sends self as an argument, which doesn't match the order function signature and causes all sorts of problems.

Is there any way to keep Python from sending the self in this case?

Answer:

Ideally, use Python's staticmethod decorator in the declaration of the _VIEW attribute. After all, what you want is to access it statically (regardless of the OrderTests test class instance).

# TESTS.PY
class OrderTests(TestCase, ShopTest):
    _VIEW = staticmethod(views.order)

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        self._VIEW(request, **{'sku': order.sku})


# VIEWS.PY
def order(request, sku)
     ...
Scroll to Top