1
\$\begingroup\$
  1. I want to generate a random date of birth [mm/dd/yyyy]which will represent an age equal to less than 18years old
  2. My requirement is to dynamically generate test data [date of birth] to negatively test a system which needs a date of birth (age) to be always greater than 18 years old
  3. The system which I am trying to test compares the user's input D.O.B. with the current date and calculates the age, if the age is less than 18 years old then throws an error
  4. I have tried to create the below python snippet to generate a date of birth [for age less than 18 years old] and kindly need expert opinions to know if this is indeed the correct/best approach?
start_date = date.today() - relativedelta(years=18) end_date = date.today() time_between_dates = end_date - start_date days_between_dates = time_between_dates.days random_number_of_days = random.randrange(days_between_dates) random_date = start_date + datetime.timedelta(days=random_number_of_days) month = random_date.month day = random_date.day year = random_date.year # Once I have the the day, month, year then I can convert it in ISO date format as per my needs 
\$\endgroup\$
2
  • \$\begingroup\$ Do you really need random dates for testing? Usually, good tests focus on the boundary conditions, so you'd want to use dates including exactly 18 years, the same ±1day, and at the other extreme, today and tomorrow. A really good test will allow the "today" date to be passed into the code under test, so we can check the right behaviour when the birthday or the present day is 29 February. \$\endgroup\$ Commented Mar 27, 2021 at 9:28
  • 1
    \$\begingroup\$ @TobySpeight what you say is true for a generic unit test. For a fuzzer you actually do need random input. It's not clear that the OP appreciates this distinction mind you. \$\endgroup\$ Commented Mar 27, 2021 at 13:43

1 Answer 1

6
\$\begingroup\$

datetime.date has a .replace() method that returns a copy with some values changed. This can be used to get the date 18 years ago.

end_date = date.today() start_date = end_date.replace(year=end_date.year - 18) 

datetime.date.toordinal returns an integer corresponding to the date. The ordinals for the start and stop date can be used in random.randint() to pick a random ordinal, which can then be converted back into a date with date.fromordinal().

random_date = date.fromordinal(random.randint(start_date.toordinal(), end_date.toordinal())) 

date.isoformat() returns a date a as YYYY-MM-DD. Or use date.strftime() for more control over the date format. In an f-string, a date is converted to ISO format, or a format string can be used: f"{random_date:%a, %b %d, %Y}" -> 'Sat, Dec 14, 2013'.

If you want to generate a list of dates in the range, use random.sample() with a range object.

NUMBER_OF_DATES = 10 date_range = range(start_date.toordinal(), end_date.toordinal()) random_dates = [date.fromordinal(o) for o in random.sample(date_range, NUMBER_OF_DATES)] 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.