Test assertion fail
Test assertion fail
I've wrote a function which export dictionary as a string into csv file.
In my assignment I'm instructed to export this dict as one string (I took keys from dict and separated them with commas - no spaces). I have a file which test this function - I'm here so of course it fails. However when I print output of that function it looks like in my assignment e.g sth1,sth2,sth3,sth4. What I'm missing here?
Here is my code:
def export_stock(stock, filename="export.csv"):
with open(filename, "w+") as exportation:
a = list(stock.keys())
a = ",".join(a)
exportation.write(a)
and here is test func:
def test_export(self):
export_stock({'sth1': 3, 'sth2': 1, "sth3": 1},
"export.csv")
with open("export.csv", newline='') as csvfile:
expected = ["sth1", "sth2", "sth3", "sth1", "sth1"]
expected.sort()
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
row.sort()
self.assertListEqual(expected, row)
expected
"sth1"
And why are you reading from a different file from what you wrote to?
– user2357112
Jul 1 at 22:38
I've mispronounced file should have the same name but test still fails
– user9546355
Jul 1 at 22:46
That's because your program isn't doing what your assignment wants. I assume it wants you to look at the value attached to the key, then repeat the key that number of times in the row. Since 'sth1' has a value of 3, it should be repeated three times.
– wowserx
Jul 2 at 3:42
1 Answer
1
You're passing in the filename test_stock_export.csv when you call your function, so it will export the results to that file. In your test, you open export.csv and check for the results in there. I think you meant to open test_stock_export.csv in your test.
test_stock_export.csv
export.csv
test_stock_export.csv
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why does your
expectedlist have"sth1"in it 3 times?– user2357112
Jul 1 at 22:37