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!

Ignacio Such
  • 129
  • 1
  • 8
  • 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`. – Bharel Jan 10 '22 at 02:47
  • 1
    The `&` in your `if` is not the right code. You probably want `and`. See [Boolean operators vs Bitwise operators](https://stackoverflow.com/q/3845018/2745495). And you probably want to also use `isinstance` on the `file_path` check, as in `isinstance(file_path, str)`. – Gino Mempin Jan 10 '22 at 02: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. – Ignacio Such Jan 10 '22 at 03:00

1 Answers1

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)):
Bharel
  • 23,672
  • 5
  • 40
  • 80