Hello Dojo Community
I'm working on a unit test for a simple method that evaluates a string expression and returns the value of that expression. i.e.: Input: "1 + 2" returns 3 as double.
I did the unit test and when I run/debug them I see the correct values and the test passes, however when I push the driver through the pipeline the test seems to fail.
I've tried multiple ways to assert the result:
- Parsing to string with the same number of decimals
- Rounding both values with the same number of decimals
- Getting the difference of both values and asserting that is below a certain value
- Asserting that the doubles are equal with a value of tolerance set
This is one example of code I've done:
string goodOperation = "0.0840435203018592- 1.5";
double goodOperationResult = -1.41596;
goodOperationResult = Math.Round(goodOperationResult, 5);
var goodOperationTestResult = TrendingDataHelper.Evaluate(goodOperation);
goodOperationTestResult = Math.Round(goodOperationTestResult, 5);
Assert.AreEqual(goodOperationResult, goodOperationTestResult, 0.00001);
All of those are working in the code and the test passes correctly and when I debug I see the correct values, but all fail on the pipeline.
Is there something I'm doing wrong or is a better way to assert doubles?
Thanks in advance!
Hi Luis, could the failure be related to regional formatting? Some areas of the world use the dot '.' as decimal separator, while others use the comma ','. If the pipeline server has a different regional setting from the machine you used to test locally that could be the cause. The best way is to make your code robust for this by specifying the expected format when parsing strings.
Hi Michiel and Pedro
I updated the code to add the InvariantCulture on the method Pedro indicated and it worked.
Thanks for the help! I’ll keep in mind the formatting when dealing with these cases.
Indeed, this is the issue, in your implementation you use a DataTable to perform the expression evaluation. There you set the local to CultureInfo.InvariantCulture. However, when parsing the result you do the following:
var result = double.Parse((string)row[“expression”]);
By changing this to:
var result = double.Parse((string)row[“expression”], CultureInfo.InvariantCulture);
the test should now also succeed on the Jenkins server.