Running the code below I get
E TypeError: unbound method make_request() must be called with A instance as first argument (got str instance instead)
I dont want to set make_request method as static, I want to call it from an instance of an object.
The example http://pytest.org/latest/fixture.html#fixture-function
# content of ./test_smtpsimple.py
import pytest
@pytest.fixture
def smtp():
import smtplib
return smtplib.SMTP("merlinux.eu")
def test_ehlo(smtp):
response, msg = smtp.ehlo()
assert response == 250
assert "merlinux" in msg
assert 0 # for demo purposes
My code
""" """
import pytest
class A(object):
""" """
def __init__(self, name ):
""" """
self._prop1 = [name]
@property
def prop1(self):
return self._prop1
@prop1.setter
def prop1(self, arguments):
self._prop1 = arguments
def make_request(self, sex):
return 'result'
def __call__(self):
return self
@pytest.fixture()
def myfixture():
""" """
A('BigDave')
return A
def test_validateA(myfixture):
result = myfixture.make_request('male')
assert result =='result'
You can try replacing your last two methods as: –
myfixtureis the function object. To invoke the function you need a bracket on it. So,myfixture().Now in
myfixture()method,return Aagain returns the class object. In order to return aninstanceof class A, on which you will call your method, you need to return eitherA()or just return theA('BigDave')you are using there.So, now your
test_validateAmethod will get an instance of classAfrommyfixturemethod, on which you are invoking the method, thus passing theselfas first argument.