I am writing a test for a method and I want to validate that the method returns a specific type. However, when I try this I get an error.
def search_emails(mail): data = mail.uid('search') raw_email = data[0][1] return raw_email
The type(raw_email) is: <class 'bytes'>
When I run this test:
def test_search_emails_returns_bytes(): result = email_handler.search_emails(mail) assert type(result) == "<class 'bytes'>"
I get this error. How can I state the assertion so the test will pass? Or is there a better way to write the test?
E assert <class 'bytes'> == "<class 'bytes'>"
use ` assertThat(actualException, not(instanceOf(BaseClass. class))); ` for negative test.
assertIsInstance() in Python is a unittest library function that is used in unit testing to check whether an object is an instance of a given class or not. This function will take three parameters as input and return a boolean value depending upon the assert condition.
You can do -- assert isinstance(obj, basestring) -- str and unicode both inherit from basestring, so this will work for both.
You can use the is
operator to check that a variable is of a specific type
my_var = 'hello world' assert type(my_var) is str
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With