I'm writing unit tests for the following the following class.
module1/node_base.py
@dataclass
class NodeBase(metaclass=ABCMeta):
account: AccountBase
name: str
ec2_id: str = ''
def start(self):
self.account.session.resource('ec2').instances.filter(
InstanceIds=[self.ec2_id]).start()
self.account.session
is AWS session. How to write the test function? (to check if filter()
is called with parameter InstanceIds=[self.ec2_id]
and start()
is called?)
test_start.py
from module1.node_base import NodeBase
class Node(NodeBase):
'''Node'''
@pytest.fixture
def sut() -> Node:
session = Mock()
def get_resource():
instances = Mock()
def get_filter(filters):
def get_start():
pass
return get_start
instances.filter = get_filter
return instances
session.resource = get_resource()
account = Mock()
account.session = session
return Node(account=account, name='Test')
def test_start(sut):
sut.start()
assert sut.account.session.resource('ec2').instances.filter.call_count == 1
assert sut.account.session.resource('ec2').instances.filter().start.call_count == 1
It seems very verbose to setup the fixture. Is it a simpler approach to test it?
How to test if filter() is called with a particular parameter ['some_id']
?