Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert that a type equals a given value

Tags:

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'>" 
like image 743
analyticsPierce Avatar asked Jul 25 '16 17:07

analyticsPierce


People also ask

How do you assert a class type in Java?

use ` assertThat(actualException, not(instanceOf(BaseClass. class))); ` for negative test.

What is assert Isinstance in Python?

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.

How do you assert a class in Python?

You can do -- assert isinstance(obj, basestring) -- str and unicode both inherit from basestring, so this will work for both.


1 Answers

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 
like image 79
Zac Crites Avatar answered Oct 23 '22 13:10

Zac Crites