Kevin Price
Kevin Price

Reputation: 429

How to use variable in feature file

How can I use variables in my feature file? Specifically, I need to use dateTime.now. Ideally, something like...

Given the API returns items for "dateTime.now"
when my function is run 
then I want that data in my database

And in my acceptance test file...

[Given("The API returns line items for (.*)")]

Is this the proper way to go about this? I'm unsure of how to use variables in my feature file. I want my acceptance test to use the current date.

Upvotes: 2

Views: 4063

Answers (3)

user1207289
user1207289

Reputation: 3253

One way , easiest imo, to get current dateTime in your test is [StepArgumentTransformation] and then you can extract date or other things out of it . This way you can use [StepArgumentTransformation] in other steps in your Gherkin like below resulting in less code

Given the API returns items for currentTime

Given the database1 returns items for rightNowTime

Given the database2 returns items for presentTime

Above is just ex , but basically match it with whatever string variable you like

    public class binding {

            public DateTime datetime;

[Given(@"the API returns items for (.*)")]
[Given(@"Given the database1 returns items for (.*)")] 
[Given(@"Given the database2 returns items for (.*)")] 

 public void currentDatetime(DateTime dt)
            {          
                log.Info("current time: " + datetime);
                 log.Info("current date: " + datetime.Date);

            }

          [StepArgumentTransformation]
           public DateTime convertToDatetime(string c)
            {
                 datetime = DateTime.Now;
                 return datetime;
            }

    }

The above code logs current time and current date three times

Upvotes: 1

Jeroen Lamberts
Jeroen Lamberts

Reputation: 301

I partially agree with Greg Brughardts answer. Because the feature file is something you can share with business stakeholders (or other non-IT persons in your organisation), it would make more sense to use 'real world' language in your feature files. Also, when you would pass this to a reporting tool at the end of the test it would be more readable.

I'd approach it like this. The switch statement makes it easy to add other types of dates with real world language:

[Given("The API returns line items for '(.*)'")]
public void GivenTheAPIReturnsItemsForTime(string mydate)
{
     switch (mydate)
     {
          case:"the current date":
              HandleApiDateTime(DateTime.Now.ToString("dd-MM-yyyy"))
              // pass the current date to the Api handler
              break;
          case:"yesterday":
              HandleApiDateTime(DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy"))
              // pass yesterdays date to the Api handler
              break;
          default:
              Console.Writeline("I didnt recognize this command");
              // or other error handling
              break;
     }
}

private void HandleApiDateTime(DateTime mydate)
{
    // do your api magic with a date object
}

Your feature file could then look like

Given the API returns items for 'yesterday'
when my function is run 
then I want that data in my database

Upvotes: 2

Greg Burghardt
Greg Burghardt

Reputation: 18783

The easiest way is to write a step specific for returning lines items for "right now":

Given the API returns items for right now

You can call the other version of the step from the new version:

[Given(@"the API returns items for right now")]
public void GivenTheAPIReturnsItemsForRightNow()
{
    GivenTheAPIReturnsItemsFor(DateTime.Now);
}

This avoids code duplication between steps.

Upvotes: 5

Related Questions