SpecFlow: System.FormatException: Input string was not in a correct format
Asked Answered
H

3

8

I was getting the following error when running my tests using SpecFlow:

System.FormatException: Input string was not in a correct format.

And it took me a while to work out why it was happening.

Homans answered 1/4, 2015 at 14:38 Comment(1)
I've got the same error, and the complete lack of helpful stack trace (or any clue as to where it's happening) from SpecFlow doesn't help.Regnant
H
9

It ended up being because I had omitted the single quotes in one of my step definitions, for example:

[Then(@"Something adds up to a quantity of (.*)")]

when it should have been

[Then(@"Something adds up to a quantity of '(.*)'")]

Note the single quotes around (.*)

Homans answered 1/4, 2015 at 14:38 Comment(0)
R
2

Another thing to look for when running into this problem is to verify parameters.

When I update the thing by Id (<Id>)

Examples:
  | Name |
  | 1    |

The problem here is that "Id" does not match "Name" in the examples table

Redman answered 28/12, 2020 at 19:37 Comment(0)
W
0

This may be INDEED a very time consuming issue, particulary when debugging step-by-step and only getting the System.FormatException: Input string was not in a correct format when we get thru the last scenario line, although the offending binding code was related to a prior one.

In my case, I had initialy created a feature with several integers, which generated feature StepDefinitions code using INT parameters, and they runned fine. However, later, I've decide to test an edge case with a decimal value, but I forgot to revise feature StepDefinitions code according, namelly changing some parameters from INT to DECIMAL. That's when I run into troubles.

The feature

Feature: Calculator

Finds de differente between two numbers

Scenario: Subtract two numbers
    Given the first number is 12.5
    And the second number is 2.5
    When the two numbers are subtracted
    Then the result should be 10

The feature StepDefinitions's code

using System;
using TechTalk.SpecFlow;

namespace ProcessamentoTests.StepDefinitions
{
    [Binding]
    public class CalculatorStepDefinitions
    {
        [Given(@"the first number is (.*)")]
        public void GivenTheFirstNumberIs(decimal p0)
        {
            // Ignore for now
        }

        [Given(@"the second number is (.*)")]
        public void GivenTheSecondNumberIs(int p0)
        {
            // Ignore for now
        }

        [When(@"the two numbers are subtracted")]
        public void WhenTheTwoNumbersAreSubtracted()
        {
            // Ignore for now
        }

        [Then(@"the result should be (.*)")]
        public void ThenTheResultShouldBe(int p0)
        {
            // Ignore for now
        }
    }
}

The solution is...

In this case, both p0 parameters on GivenTheSecondNumberIs and ThenTheResultShouldBe should be changed do DECIMAL type.

Therefore, until Specflow's team (*) manages a way to provide better context on parameter parsing issues, all scenario parameter bindings should be revised.

(*) Bear in mind that, given Specflow is a beloved open source project, everyone (including me) may be part of its team.

Wan answered 7/7, 2022 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.