Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 0773634

Browse files
committed
[test] Fix flaky test that occurs when 2 consecutive generated date are not in the same second
1 parent 727a64e commit 0773634

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

tests/01-unit/utils/test_opencti_stix2.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ def test_format_date_with_tz(opencti_stix2: OpenCTIStix2):
6767
my_date_str = "2021-03-05T00:00:00.000Z"
6868
assert my_date_str == opencti_stix2.format_date(my_date)
6969
assert my_datetime_str == opencti_stix2.format_date(my_datetime_str)
70-
assert (
71-
str(
72-
datetime.datetime.now(tz=datetime.timezone.utc)
73-
.isoformat(timespec="seconds")
74-
.replace("+00:00", "")
75-
)
76-
in opencti_stix2.format_date()
77-
)
70+
71+
# Test the behavior of format_date() when called without arguments.
72+
# Since it relies on the current time, avoid flaky results by comparing only up to the seconds, using dates generated immediately before and after the function call.
73+
my_now_date_1 = datetime.datetime.now(tz=datetime.timezone.utc).isoformat(timespec="seconds").replace("+00:00", "")
74+
stix_now_date = opencti_stix2.format_date()
75+
my_now_date_2 = datetime.datetime.now(tz=datetime.timezone.utc).isoformat(timespec="seconds").replace("+00:00", "")
76+
assert ((str(my_now_date_1) in stix_now_date) or (str(my_now_date_2) in stix_now_date))
77+
7878
with pytest.raises(ValueError):
7979
opencti_stix2.format_date("No time")
8080

0 commit comments

Comments
 (0)