🧑‍💻 Why Should Developers Automate? 🤖

January 4, 2025

🧑‍💻 Why Should Developers Automate Their Code? 🤔🤖

We run unit, integration, and functional tests, yet bugs still pop up in our apps! 🪲 I'm here to show you a fun solution for those pesky QA tickets that bounce back to dev and even those sneaky bugs that make it to production 😅.

This is key to speeding things up ⚡, guaranteeing quality, and most importantly, saving money 💸. A simple automation that takes less than an hour can save us tons of headaches 🤯 and catch bugs way earlier! 🏆

👉 Here are three key areas where automation can make a huge difference:

✅ Automating HTTP Status Validations

HTTP verbs like GET, POST, PUT, PATCH, DELETE are the backbone of APIs. Automating tests for HTTP responses (e.g., 200, 201, 204, 400, 404) makes sure every endpoint behaves the way we expect. 🚀

Here's an example of validating a POST response:

[Fact]
public async Task Given_Valid_Request_When_Post_Then_Return_Created()
{
    var postPayload = new AppointmentPostBody("New Appointment", DateTime.UtcNow, DateTime.UtcNow.AddHours(1), "Description");
    var request = RequestHelper.PostRequest(Url.Appointments.Post, postPayload);

    var response = await Client.ExecuteAsync(request);

    Assert.Equal(HttpStatusCode.Created, response.StatusCode);
    Assert.NotNull(response.Data);
    Assert.Equal("New Appointment", response.Data.Title);
}
  

✅ Automating Event Handling Validations

Modern microservices often rely on event-driven architectures. Automating these checks ensures listeners process events as expected. For instance, when an AppointmentNotificationEvent is published, the listener updates the database with the right data. ⚙️

[Fact]
public async Task Given_Valid_Notification_Event_When_Handled_Then_Entity_Is_Updated()
{
    var eventPayload = new AppointmentNotificationEvent("600-notification", "AppointmentNotificationEvent", "Updated Title");

    await EventBus.PublishAsync(eventPayload, EventHelper.GetEventName());

    var getRequest = RequestHelper.GetRequest($"{Url.Appointments.GetById(600)}");
    var getResponse = await Client.ExecuteAsync(getRequest);

    Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
    Assert.NotNull(getResponse.Data);
    Assert.Equal(eventPayload.Title, getResponse.Data.Title);
}
  

✅ Automating Edge Case Validations

Automation also ensures your system gracefully handles edge cases. What if you try to delete a non-existent appointment? Your API should reply with 404 Not Found. Let's see how we can test that:

[Fact]
public async Task Given_Invalid_Id_When_Delete_Then_Return_NotFound()
{
    var invalidId = 99999; // Non-existent ID
    var request = RequestHelper.DeleteRequest(Url.Appointments.Delete(invalidId));

    var response = await Client.ExecuteAsync(request);

    Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
  

🔥 And that's just the start! Every project has its own key flows worth automating.

🔑 The Bottom Line

Automation is all about having confidence in your system. By automating HTTP status checks, event-driven workflows, and edge cases, you can spot bugs early, save time, and reduce QA overhead.
The result? Faster development cycles and more reliable software! 🚀

😜 Curious to see it in action? Check out the demo appointments repository
GitHub Link: https://github.com/maurogioberti/microservice-appointments

#DotNetCore #QA #Automation #Testing #UnitTesting #FunctionalTesting #CleanArchitecture