Skip to content

Instantly share code, notes, and snippets.

@glemaitre
Created July 2, 2020 09:52
Show Gist options
  • Save glemaitre/84a21cfdd70f516b6ce1d0fd4eeed02f to your computer and use it in GitHub Desktop.
Save glemaitre/84a21cfdd70f516b6ce1d0fd4eeed02f to your computer and use it in GitHub Desktop.
import pandas as pd
import pytest
def func(expected_columns):
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [1, 2, 3],
"C": [1, 2, 3]
})
diff_columns = set(df.columns.tolist()).symmetric_difference(
expected_columns
)
if len(diff_columns):
raise ValueError(
f"Discrepency regarding the columns. The following columns are "
f"either missing or not expected: {diff_columns}."
)
return df
def test_func_error():
expected_columns = set([
"A", "B", "C", "D",
])
err_msg = "Discrepency regarding the columns."
with pytest.raises(ValueError, match=err_msg):
func(expected_columns)
def test_func():
expected_columns = set([
"A", "B", "C"
])
df = func(expected_columns)
assert set(df.columns.tolist()) == expected_columns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment