0

I want a test to fail when the if condition below becomes false.

last_url = self.browser.current_url

if 'https://***/news/' in last_url:
    print("test passed")
else:
    #Test should fail if it reaches here
    print("test failed")

How can i achieve that?

2

You can use

assert 'https://***/news/' in last_url

You can try it in Python console:

>>> last_url = 'https://***/news/page.jsp'
>>> assert 'https://***/news/' in last_url
>>> last_url = 'https://***/no-news/page.jsp'
>>> assert 'https://***/news/' in last_url
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
  • 1
    Nice, concise answer. +1 – Brian Jul 15 at 17:12
1

You can use self.fail(msg="…").

-1

You can use Asset in this case, below is the dummy code try this. I have used Java based code here you can, you can change as the language which you are using.

int isURLMatching =0;

if 'https://***/news/' in last_url:
    print("test passed")
isURLMatching++;
Assert.assertTrue("message", isURLMatching>0);

else:
    #Test should fail if it reaches here
    print("test failed")
isURLMatching=0;
Assert.assertFalse("message", isURLMatching==0);
  • This mixes Python with Java. – dzieciou Jul 15 at 20:15
  • 1
    @dzieciou and i think that's why i have mentioned the same in the comment above the code. Which clearly says what is required to be changed. – Sandeep Srivastav Jul 16 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.