0

I want to execute the code block under "if AssertionError:". But it is not working. My objective is for any Assertion error found for the previous assertion statements, I need to report the same in the log file. Where I have done wrong in the following code? I dont want to use try-except because using try except, I can make the above code work but the actual failures are not reported in the test report and its showing 100% pass even if there are assertion failures.

            else:
                assert context.json_response is not None
                assert float(context.json_response[0]['base_premium']) > float(1)
                assert float(context.json_response[0]['total_premium']) < float(1)
                assert float(context.json_response[0]['tax']) > float(1)
                print(context.json_response[0]["tax"])
                if AssertionError:
                    json_data = context.payload
                    sys.stdout = open("OD_Plans.log", "a")
                    print("Test case failed due to assertion error")
                    print("Previous Policy Status=" + " " + status)
                    print("expiry date=" + " " + exp_date)
                    print("Owner=" + " " + owner)
                    print("Registration Date=" + " " + reg_date)
                    print("Manufacturing Year=" + " " + man_yr)
                    print("Veichle Maker=" + " " + vchl_make)
                    print("Maker Code=" + " " + vchl_code)
                    print(json.dumps(json_data, indent=2))
  • 2
    That's not how you handle exceptions in Python. You need to use `try-except` – NotAName Oct 14 '21 at 05:38
  • I dont want to use try-except because using try except, I can make the above code work but the actual failures are not reported in the test report and its showing 100% pass even if there are assertion failures. – Apratim Chaudhuri Oct 14 '21 at 05:40
  • You can re-raise the `AssertionError` at the end of your handling code. See [this answer](https://stackoverflow.com/a/45730965/2011147). – Selcuk Oct 14 '21 at 05:42

0 Answers0