2

I need help to exit a test without getting error. I have this code:

tabla = self.driver.find_element_by_css_selector(".tablaSolicitudes")
for estado in tabla.find_elements_by_tag_name("td"):
    e = estado.text
if e.find("Multiadhesion") != -1:
    print("La solicitud " + solicitudes[i] + " se encuentra con MultiAdhesion. Finaliza el Test.")
    sys.exit()

If that condition is true, the test should end. The point is that, having it like that, i get this error:

sys.exit()
SystemExit

----------------------
Ran 1 test in 139.733s

FAILED (errors=1)

But the test is OK, so, how can I make a proper "test exit" so that i don´t get this error?.

Thank you very much!

DavidG
  • 24,279
  • 14
  • 89
  • 82
GHM
  • 115
  • 1
  • 12

3 Answers3

0

sys.exit() raises a SystemExit exception, which you can test with:

with self.assertRaises(SystemExit):
    your_method()

Now your test should succeed.

Sven
  • 2,839
  • 7
  • 33
  • 53
0

If you really want to stop the tests, then one of the ways is using force stop -f available in unittest

-f, --fail: fast Stop the test run on the first error or failure.

try running the module using -f option:

python sample.py -f

But, it won't mark the test as PASS and you should remove sys.exit() from try..except... block

If you have control on tests to run, then you can do as follows:

  1. set shouldStop = True in the method you want to exit (refer here.
  2. check after test method runs (each test method returns TestResult object to its caller), whether it is True or False, if it is True, call STOP() method. (refer here)

OR

Another option is pytest framework which provides the facility to exit the run. Add the following line wherever you want to exit:

pytest.exit("decided to stop the test run")

Reference:

  1. How to stop all tests from inside a test or setUp using unittest?

Old Answer:

replace sys.exit() as follows:

try:
    sys.exit()
except:
    print "its ok"

sys.exit() returns SystemExit exception.

I assume you are using unittest framework. A test is markd as PASS only when there is no Failure/Exception in the test method. as sys.exit() throwing exception, unittest marks it as FAIL.

What we do is, handle the exception in the test method itself, so no exception/failure is thrown, which makes unittest to mark that test method as PASS.

Community
  • 1
  • 1
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Doing this, the test tries to go on with the execution (after that code, the method keeps) :/ Thank you very much anyway! – GHM Nov 23 '16 at 17:31
  • Awesome! Despite solving it otherway, it's close to be what i was looking for. In tools like UFT, you have that sentence ExitTest and that's it, but webdriver makes it a bit more complex. Thank you all very much for your answers, you really know what you are talking about! – GHM Nov 24 '16 at 12:05
0

I'm not sure if sys defined higher up in your code but that could be why the test is having issues.

To close a Selenium browser test (which I believe you are using, inferring from the selenium-webdriver tag), normally a .close() or .quit() tag is used. Assuming sys is the browser, at the end of the your if statement, you would use sys.close or sys.quit to close the browser.

.close() will close a tab within a testing browser. .quit() will close the browser window.

If you need more help, check out Selenium Python Bindings Documentation. It's always been a big help for me when writing python/selenium tests.

Paula C
  • 169
  • 1
  • 8
  • sys only show me the option "exit", not close() or quit(), If I do self.driver.quit() it fails too. – GHM Nov 23 '16 at 17:33