Do some things same time in the Django unit test

How can I test if two users can reserve the same car simultaneously?

def test_if_two_users_can_reserve_the_same_car_simultaneously(self):
with patch.object(
    timezone,
    "now",
    return_value=make_aware(
        datetime.datetime.combine(
            datetime.date.today() + datetime.timedelta(days=1), datetime.time(10, 30, 0)
        ),
        timezone=pytz.timezone("UTC"),
    ),
):
    self.client.login(username=self.user.username, password=self.PASSWORD)
    url = reverse("booking-list")
    data = {
        "book_for": datetime.datetime.combine(
            datetime.date.today() + datetime.timedelta(days=1), datetime.time(11, 30, 0)
        ),
    }
    response = self.client.post(url, data=data)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)

with patch.object(
    timezone,
    "now",
    return_value=make_aware(
        datetime.datetime.combine(
            datetime.date.today() + datetime.timedelta(days=1), datetime.time(10, 30, 0)
        ),
        timezone=pytz.timezone("UTC"),
    ),
):
    self.client.login(
        username=self.another_user.username, password=self.PASSWORD
    )
    url = reverse("booking-list")
    data = {
        "book_for": datetime.datetime.combine(
            datetime.date.today() + datetime.timedelta(days=1), datetime.time(11, 30, 0)
        ),
    }
    response = self.client.post(url, data=data)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)

This is how I wrote it, but in the unit test, it runs line by line. So first one will create then move to second one but I want them both run at same time. (Please answer with an example)

Back to Top