This is the use case where you are expecting a task to fail during the unit test. There are cases where you would want to test for this. Use your imagination.
Anyways add this to the end of your molecule/*/converge.yml file:
---
- name: Converge
hosts: all
vars:
... omitted ...
task_that_should_fail: 'Check if web server returns 200 on localhost'
tasks:
... omitted ...
- name: Role Should Fail In This Test
block:
- name: Include my_role
ansible.builtin.include_role:
name: "my_role"
ignore_errors: true
- name: Passed is Fail
fail:
msg: "Role my_role unexpectedly successed!"
rescue:
- name: Failed is Pass
debug:
msg: "Role my_role failed when it should have!"
when:
- ansible_failed_task.name == task_that_should_fail
- name: Passed is Fail
fail:
msg: "Role my_role unexpectedly succeed!"
when:
- not ansible_failed_task.name == task_that_should_fail