How to handle failures on specific element

I have a project for creating loan applications , and its not allowed to have a duplicated application .
so I had to handle it throwgh clean up for active loan.
but
the problem sometime the cleanup method fails and that leads everything to be failed

is there a way during the run if certine element show (error : It looks like you already have an active loan for the same property. ) to do a full cleanup before the next test scenario ?
I tried test listeners but not sure what hook needed !

It sounds as though you are supplying the same data for each test run.

I think you have two choices:

  1. Use the web app itself to delete the loan application, if that facility is available.
      - this option will slow down your test(s).

  2. Don’t use the same data twice. You can do this by generating random data, most especially for the “key fields” which are used to identify a loan application record in the database.

If you are expected to “clean up” after test runs, then use option 1.

If not, then use option 2.

Other than that, it does not matter which option you choose.

the client created users with specific data to be able to create loans on test environment
thats why the duplication case :frowning:

So, you are saying, the data is “fixed” - you can’t change it. Correct?

In which case, you are stuck with option 1. But this is not ideal. By far the best solution is to run a database clean up script before the test suite is executed.

am running it currently after each test case through the test suite
also handled if the clean up step at anytime failed , then it should catch the failure by retry clean up from another place

That is the problem you will get if you “mix concerns”.

ONE test should do ONE thing (e.g. Add a loan application to the system)

Other tests can test the loan deletion part.

If you proceed like that, then your testing will be more meaningful.