Rasa Unit-Test how to set tracker attribute during test

Hello,

I am new to Rasa and didn’t really find anything in the documentation to UnitTests. I want to write a test for my custom action which depends on the entity_confidence. I am using the pytest library, which I took from this example rasa-demo/test_actions.py at main · RasaHQ/rasa-demo · GitHub

I want to set the entity_confidence of the tracker attribute, but it’s not working like I thought. Below is my code

this is the test


@pytest.mark.parametrize(
    "confidence,expected_events",
    [
        (0.9, [SlotSet("keyword_confirmed_slot", True)]),
    ],
)
def test_confirm_mapping(
    tracker: Tracker,
    dispatcher: CollectingDispatcher,
    domain: DomainDict,
    confidence: float,
    expected_events: List[EventType],
):
    tracker.latest_message['entities'] = [{
            "entity":"keyword",
            "confidence_entity":confidence,
            "value":"snakes",
         }]
    print(tracker.latest_message)
    action = actions.ActionConfirmKeyword()
    actual_events = action.run(dispatcher, tracker, domain)
    assert actual_events == expected_events
my conftest.py 
@pytest.fixture
def tracker() -> Tracker:
    """Load a tracker object"""
    with open("tests/data/keyword_confirm.json") as json_file:
        tracker = Tracker.from_dict(json.load(json_file))
    return tracker


@pytest.fixture
def dispatcher() -> CollectingDispatcher:
    """Create a clean dispatcher"""
    return CollectingDispatcher()


@pytest.fixture
def domain() -> DomainDict:
    """Load the domain and return it as a dictionary"""
    domain = Domain.load("domain.yml")
    return domain.as_dict()

I also wondered how and when the conftest.py gets executed, but thats not my important question

and lastly my custom action

class ActionConfirmKeyword(Action):
    def name(self) -> Text:
        return "action_confirm_keyword"

    def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:

        keyword = next(tracker.get_latest_entity_values(entity_type="keyword"), None)
        entities = tracker.get_last_event_for("user").get("parse_data").get("entities")
        print(entities)
        try:
            keyword_confidence = entities[0].get("confidence_entity", 0)
        except (TypeError, IndexError):
            keyword_confidence = 0

        if not keyword:
            dispatcher.utter_message("Das habe ich leider nicht verstanden.")
            return [FollowupAction("action_correct_keyword")]

        buttons = [
            {
                "payload": '/confirm{"confirm_entity": true}',
                "title": "Ja",
            },
            {
                "payload": '/deny{"confirm_entity": false}',
                "title": "Nein",
            },
        ]

        if keyword_confidence < 0.9:
            dispatcher.utter_message(
                text=f"Du suchst nach '{keyword}', habe ich das richtig verstanden?",
                buttons=buttons,
            )
            return [SlotSet("keyword_confirmed_slot", False)]

        return [SlotSet("keyword_confirmed_slot", True)]

here my keyword_confidence is always 0, because the tracker doesnt have any entites.

Any help is very appreciated

Take a look at the examples in the test directory of the financial-demo and you’ll find examples in the data directory.

Thanks for your reply. I’ve had a look at it and set up my own json file too like that. I was wondering if what I wanted to try works together with using a json file.

If not, how do I set up different trackers from different json files during my test execution? Since I need to change the entity confidence

Code is here

alright, thank you