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?

3 Answers 3

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
  • 1
    Nice, concise answer. +1
    – Brian
    Jul 15, 2019 at 17:12
2

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);
2
  • This mixes Python with Java.
    – dzieciou
    Jul 15, 2019 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. Jul 16, 2019 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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