0

How to test (in pytest) calculations when there is no reference to check against?

Say, I have a dataframe with a and b columns. And column c is calculated using a and b. The results are float numbers.

df = pd.DataFrame({'a': [4,3,5], 'b': [11,7,13]})
df['c'] = df['a']/df['b']
anarz
  • 209
  • 4
  • 11
  • What do you mean by "no reference to check against"? – deceze Apr 24 '19 at 13:08
  • Any reference with expected correct results. I could of course create csv or txt file with correct results and check against that, but it feels it is not the right way to do it. I am quite new in unit-testing, so I might be wrong. – anarz Apr 24 '19 at 13:20
  • You'll need to create _some_ reference to test against. Whether you do that in a CSV, or you explicitly pick certain values from the data frame to explicitly compare to known good values are just implementation details and depends on how many values you need to test to be sure your test is okay. – deceze Apr 24 '19 at 13:38

1 Answers1

0

Use known values for column A and B in your test, so that you deterministically can asset on the result in column C.

Check out this post for handling the assert part: How to perform unittest for floating point outputs? - python

Jocke
  • 2,189
  • 1
  • 16
  • 24