> Markdown version of [/videos/230-using-code-generation-for-test-automation-the-fancy-way](https://www.wearedevelopers.com/videos/230-using-code-generation-for-test-automation-the-fancy-way). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Using code generation for test automation – the fancy way Stop wasting hours on brittle unit tests. Learn how auto-generating C# HTTP clients from OpenAPI specs creates lightning-fast, robust API tests with zero boilerplate. - **Speakers:** Roman Alexis Anastasini - **Event:** World Congress 2021 - **Published:** June 28, 2021 - **Duration:** 40:16 - **URL:** https://www.wearedevelopers.com/videos/230-using-code-generation-for-test-automation-the-fancy-way ## Summary The challenge with traditional unit testing is that creating and maintaining granular tests can be incredibly time-consuming and often misses complex integration gaps. Shifting away from heavy unit test coverage, this approach leans into API-level test automation powered by code generation. By leveraging NSwag within a .NET environment to generate an OpenAPI specification, developers can automatically produce robust C# HTTP clients. This drastically reduces testing boilerplate, ensures the exact client being delivered to consumers is strictly validated, and provides a powerful framework for writing API tests effortlessly.\n\nImplementing this strategy involves pairing an xUnit testing project with WebApplicationFactory to run an in-memory test server. Utilizing the auto-generated HTTP client, developers can make direct API calls in tests and seamlessly inspect responses using FluentAssertions. Because the generated client inherently handles Data Transfer Objects (DTOs), strict RFC 3339 timezone serialization, and edge-case parameters out of the box, building extensive test scenarios becomes trivial. Rather than manually parsing API responses, validation failures and HTTP errors are cleanly surfaced as strongly-typed API exceptions.\n\nThis shift toward generated-client testing leads to a highly resilient and refactor-friendly codebase; renaming a controller endpoint immediately triggers compiler errors in the test suite, preventing out-of-sync API contracts. This methodology mandates writing exceptionally precise controller code—explicitly decorating expected status codes and return types—so the Swagger document accurately informs client generation. By additionally mocking external dependencies and replacing bulky Dockerized instances with in-memory database alternatives, end-to-end local execution times drop from minutes to mere seconds, drastically reducing developer friction and maintaining pristine statement coverage. **Keywords:** api test automation, code generation tooling, c# client generation, nswag configuration, openapi specification, dotnet web api testing, webapplicationfactory framework, fluent assertions library, auto-generated http client, rfc 3339 compliance, data transfer objects, edge case validation, test-driven refactoring, swagger documentation precision, mocking external dependencies, in-memory database replacement, test execution optimization ## Chapters 1. **Replacing Swashbuckle with NSwag for API testing** (00:41) — Swapping Swashbuckle for NSwag documentation streamlines the initial setup process for generating API clients. 1. **Generating a Swagger JSON file during the build process** (04:21) — Configuring the project file automatically generates an OpenAPI specification immediately after the build process completes. 1. **Creating and configuring the unit test project** (07:20) — Adding an xUnit test project and referencing Microsoft MVC packages prepares the environment for automated testing. 1. **Generating a C# API client from the Swagger JSON** (09:25) — Automating C# client generation during the build phase seamlessly provides typed API endpoints to the test project. 1. **Writing API-level HTTP tests instead of unit tests** (11:18) — Replacing cumbersome unit tests with API-level tests using generated HTTP clients significantly broadens functional test coverage. 1. **Handling API endpoint changes safely with generated clients** (14:24) — Automatically generated clients provide compile-time safety by instantly flagging any renamed or modified HTTP endpoints. 1. **Validating requests and responses using data transfer objects** (15:25) — Leveraging generated data transfer objects enables built-in model validation and straightforward serialization inside test setups. 1. **Testing edge cases and validation errors with API exceptions** (20:33) — Asserting against automatically thrown API exceptions provides a reliable way to verify bad requests and validation errors. 1. **Achieving high test coverage efficiently with API tests** (22:39) — Utilizing automated API integration checks allows teams to efficiently reach minimum statement coverage thresholds. 1. **Creating precise controller code for accurate client generation** (25:42) — Writing explicit controller configurations ensures accurate OpenAPI specifications and reliable client generation. 1. **Switching from Docker databases to in-memory databases** (27:54) — Moving from resource-heavy Docker containers to in-memory databases dramatically reduces test execution times. 1. **Understanding unit testing overhead and code coverage metrics** (28:53) — Maintaining granular unit tests consumes excessive resources compared to using broad API-level integration checks. 1. **Detecting breaking API changes in CI pipelines** (31:44) — Running integration tests with older generated clients helps prevent accidental backward compatibility breaks in CI pipelines. 1. **Managing test state and setup dependencies** (33:48) — Utilizing web application factories and abstract base classes cleanly handles state initialization for individual test classes. 1. **Mocking external service dependencies in API tests** (35:28) — Abstracting external dependencies behind mocked interfaces isolates API tests while preserving full coverage during integration runs. 1. **Unexpected test automation benefits and execution speed improvements** (37:16) — Reusing API clients intended for external consumers significantly reduces test code verbosity and improves overall productivity. ## Related Moments - [Testing API endpoints visually using Swagger UI](https://www.wearedevelopers.com/videos/385-quarkus-a-bliss-for-developers) (from "Quarkus. A Bliss for developers") - [Automating documentation and code generation with OpenAPI standards](https://www.wearedevelopers.com/videos/759-awful-apis-a-history-lesson-in-industry-mistakes-and-mishaps) (from "Awful APIs: A History Lesson in Industry Mistakes and Mishaps") - [Bootstrapping a test-driven asp.net web api](https://www.wearedevelopers.com/videos/114-why-you-must-use-tdd-now) (from "Why you must use TDD now!") - [Implementing contract-first API design with OpenAPI and SwaggerHub](https://www.wearedevelopers.com/videos/83-enabling-automated-1-click-customer-deployments-with-built-in-quality-and-security) (from "Enabling automated 1-click customer deployments with built-in quality and security") - [Implementing automated API and workflow testing pipelines](https://www.wearedevelopers.com/videos/100340-ai-driven-development) (from "AI Driven Development") - [Applying test-driven principles to APIs](https://www.wearedevelopers.com/videos/222-test-driven-development-with-javascript) (from "Test Driven Development with JavaScript") ## Related Articles - [Liuba Gonta and Yuliya Khadasevic - GitHub Copilot Beyond the Basics - 10 Ways to Elevate Your Coding](https://www.wearedevelopers.com/magazine/490-liuba-gonta-and-yuliya-khadasevic-github-copilot-beyond-the-basics-10-ways-to-elevate-your-coding) - [13 AI Tools for Developers](https://www.wearedevelopers.com/magazine/302-13-ai-tools-for-developers) - [Dev Digest 120 - Apple and peers](https://www.wearedevelopers.com/magazine/455-dev-digest-120-apple-and-peers) - [Transforming Software Development: The Role of AI and Developer Tools](https://www.wearedevelopers.com/magazine/527-transforming-software-development-the-role-of-ai-and-developer-tools) ## Related Jobs - [QA Automation Engineer](https://www.wearedevelopers.com/jobs/ext/1414297-qa-automation-engineer) at **Bitpanda** - [AI Software Engineer (Germany)](https://www.wearedevelopers.com/jobs/48317-ai-software-engineer-germany) at **Sunhat** - [Staff Frontend Engineer](https://www.wearedevelopers.com/jobs/48313-staff-frontend-engineer) at **SMG Swiss Marketplace Group** - [Senior Software Engineer, Client Apps Platform](https://www.wearedevelopers.com/jobs/ext/1773893-senior-software-engineer-client-apps-platform) at **GitHub** - [iOS Engineer, Swift/SwiftUI/Combine (B2C Broker)](https://www.wearedevelopers.com/jobs/ext/1611832-ios-engineer-swift-swiftui-combine-b2c-broker) at **Bitpanda** - [Software Tester (f/m/d)](https://www.wearedevelopers.com/jobs/44712-software-tester-f-m-d) at **Power Plus Communications**