0

Here is my Python code.

try: ct("table_that_does_not_exist", "database_that_does_not_exists")
except r.errors.ReqlOpFailedError as e: self.fail()

ct is a function to check if a table is exist in a database. ct returns True if a table exists in the database and False for otherwise.

try: ct("table_that_does_not_exist", "database_that_does_not_exists") is trying to check a non-existent table in a non-existent database. This should returns an error of this.

rethinkdb.errors.ReqlOpFailedError: Database `database_that_does_not_exists` does not exist in:
r.db('database_that_does_not_exists').table_list().contains('table_that_does_not_exist')

However, the error is somewhat not capture in except r.errors.ReqlOpFailedError as e: self.fail() and returns an AssertionError.

I expect this to pass the test, since ct("table_that_does_not_exist", "database_that_does_not_exists") will return r.errors.ReqlOpFailedError. But I got this instead.

======================================================================
FAIL: test_check_table (__main__.test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 61, in test_check_table
    try: ct("table_that_does_not_exist", "database_that_does_not_exists")
rethinkdb.errors.ReqlOpFailedError: Database `database_that_does_not_exists` does not exist in:
r.db('database_that_does_not_exists').table_list().contains('table_that_does_not_exist')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                   

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 62, in test_check_table
    except r.errors.ReqlOpFailedError as e: self.fail()
AssertionError

----------------------------------------------------------------------
Ran 7 tests in 0.050s

FAILED (failures=1)

The error is match but not proceed through except.

What is wrong with my test?

5
  • is this pytest? Commented Feb 7, 2017 at 20:19
  • No, it is the native unittest in Python 3.5. Commented Feb 7, 2017 at 20:20
  • What do you expect self.fail() to do? Commented Feb 7, 2017 at 20:25
  • Hey there! I was following this, stackoverflow.com/questions/4319825/…. I am still learning unit testing in general. What I want is the is to pass the test if certain exception happens. In this case I want to pass the test if r.errors.ReqlOpFailedError happens. Commented Feb 7, 2017 at 20:31
  • Is there a reason I got downvoted :/? Commented Feb 7, 2017 at 20:38

1 Answer 1

1

I believe your general approach to this is incorrect. Your exception checking code should look like this:

with self.assertRaises(r.errors.ReqlOpFailedError):
    ct("table_that_does_not_exist", "database_that_does_not_exists")
Sign up to request clarification or add additional context in comments.

5 Comments

Hey! This is solve my problem. But how can I have the something like "If an error is NOT happening, pass the test."?
just use the ct("table_that_does_exist", "database_that_does_exists") snippet without with self.asserRaises(...)? :)
So no assertion necessary? I thought the point of unit testing in Python is that you need to refer back to those assert functions in unittest.TestCase. If I just use ct("table_that_does_exist", "database_that_does_exists") I can just do this in any class I want and not necessarily to be in a class that inherits from unittest.TestCase right?
if you are using unittest framework, I believe unittest.TestCase is necessary. But in pytest it's not. Regarding your question: you should find a way to "assert" that your table or database is actually exist. Something like self.assertTrue(table_exists), but how do you populate this table_exists variable is an open question.
Hey there, I found that try: c()\except r.errors.ReqlDriverError as e: self.fail() is good way to check if a function went through. If exception happens then it automatically failed the test.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.