0

I am trying to make some tests with my current code but I am struggling with the assertTrue and assertFalse as I cannot pass any test or I get numerous errors during the tests in Python. So basically, most of my functions are something like this:

import pandas as pd

def read_csv_pandas(file_path, column_name):

  if not isinstance(column_name, str) & (file_path, str):
      return False
  else:
    start = time.time()
    df = pd.read_csv(file_path, ';')
    print("Listof values:", df[column_name].tolist())
    end = time.time()  
    n_lines_pandas = df.shape[0]  
    total_time = end - start 
    return total_time, n_lines_pandas  

This is my test code so far:

class TestIsDottedQad(unittest.TestCase):

    def test_read_csv_pandas(self):
    self.assertTrue(read_csv_pandas("data/artists_norm.csv", 'name'))
    self.assertFalse(read_csv_pandas("data/albums_norm.csv", 235))

How can I set my code to make my test pass?

I tried also to use if not isinstance(column_name, str) & (file_path, str): return value else: ... but nothing happened.

If there is a simple way to assertTrue/False I would also like to hear about it!

Thank you in advance!

3
  • 1
    I'm sorry, but we don't know the test, we don't know the actual function, and we don't know what is isinstance(foo,bar)... blah blah. Commented Jan 10, 2022 at 2:47
  • 1
    The & in your if is not the right code. You probably want and. See Boolean operators vs Bitwise operators. And you probably want to also use isinstance on the file_path check, as in isinstance(file_path, str). Commented Jan 10, 2022 at 2:57
  • That was it Gino, now it is perfect. Thank you! I had to use the and and the isinstance(file_path, str) as you mentioned. Commented Jan 10, 2022 at 3:00

1 Answer 1

1

Your isinstance check is incorrect. As @gino-mempin wrote, & is not used for boolean operations. Use and instead. See the following code:

if not (isinstance(column_name, str) and isinstance(file_path, str)):
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.