3

Are there any good/best practices for dealing with floating point values when checking results with the fit framework?

Our application does many calculations. In most situations checking values a precision of more than 6 or 8 digits does not make any sense. So I would like to specify the precision that are used when comparing expected and actual values.

My brute force solution would be to set a precision value and then compare values in my ColumnFixture containing the test cases.

This would result in something like:

!|info.fitnesse.fixturegallery.ColumnFixtureTest|
|firstSummand|secondSummand|precision|expectedResult|calculateSumAndCheck()|
|2.1|3.3|0.0001|5.3|true|

The major drawback of this approach would be that I would not be able to use the compare features of fit but write my own ones.

4

1 に答える 1

2

浮動小数点型に一貫した文字列形式を使用して、文字列同士を比較できます。とにかく、Fit は値に対して実際に ToString() を実行し、Wiki のテキストと比較しているため、独自の文字列フォーマットを提供する場合、Fit の組み込みの比較を変更する必要はありません。

言語は指定しませんでしたが、たとえば C# では、"ToStringForComparison()" などと呼ばれる静的ヘルパー メソッドまたは浮動小数点型 (float、double など) の拡張メソッドを使用できます。すべてのテスト ハーネスは、テストで比較する値のプロパティ アクセサーでこれを使用する必要があります。

編集:

精度を制御したい場合は、精度が重要でない値のデフォルトを維持しながら、精度の値を書式設定関数に渡すことでそれを行うことができます。

于 2009-11-07T02:28:18.180 に答える