Sawitree Cha
Sawitree Cha

Reputation: 199

Robot Framework - TypeError: relativedelta only diffs datetime/date

I get the TypeError: relativedelta only diffs datetime/date after execute code

This is my robot code:

Test calculate age
[Tags]   Test
${cal}      AgeTest   1988, 10, 1

This is my python code:

from datetime import date
from dateutil.relativedelta import relativedelta

def AgeTest(dob):
age = relativedelta(date.today(), dob)
print (age)
return age.years, age.months, age.days

How to fix it.

Upvotes: 0

Views: 6053

Answers (2)

Sidara KEO
Sidara KEO

Reputation: 1709

The datatype of dob you're passing is String but in python is need tuple So you need to convert your string to tuplefirst, Just Change following below

def AgeTest(dob):
    dobnew = tuple(map(int, dob.split(',')))
    age = relativedelta(date.today(), date(*dobnew))
    return age.years, age.months, age.days

Upvotes: 3

mfrackowiak
mfrackowiak

Reputation: 1304

I haven't worked with Robot Framework, but it looks to me as it did not recognize your input as a proper date; looking at docs, possibly formatting it as 1988-10-1 could help. On the other hand, if dob is a tuple, you could make it to a date object easily:

age = relativedelta(date.today(), date(*dob))

Upvotes: 2

Related Questions