<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexander Osadchiy on Medium]]></title>
        <description><![CDATA[Stories by Alexander Osadchiy on Medium]]></description>
        <link>https://medium.com/@aosadchiy?source=rss-45471ab81e72------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Aw8uGjASNNel1RL1pYa0IQ.jpeg</url>
            <title>Stories by Alexander Osadchiy on Medium</title>
            <link>https://medium.com/@aosadchiy?source=rss-45471ab81e72------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 10:38:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aosadchiy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Testing With OpenAPI]]></title>
            <link>https://medium.com/@aosadchiy/testing-with-openapi-73a00f59ca16?source=rss-45471ab81e72------2</link>
            <guid isPermaLink="false">https://medium.com/p/73a00f59ca16</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[qa]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Alexander Osadchiy]]></dc:creator>
            <pubDate>Mon, 13 Apr 2020 11:14:38 GMT</pubDate>
            <atom:updated>2020-04-13T11:14:38.667Z</atom:updated>
            <content:encoded><![CDATA[<p>Quality Assurance is one of the most important sectors right now, since it directly impacts the quality of products and software we’re using. QA engineers do a lot of manual testing, which is costly and often ineffective, but test automation allows to reduce costs. API testing is key to quality test automation, and QA engineers have to carefully choose testing tools, to ensure effectiveness and cost efficiency. As I was looking for such tools, I came across Aleksei Chumagin’s report https://youtu.be/8qI1OLvF5S4 on how to automate API (Open API specification). I’ve translated the report, and I’d like to publish it here.</p><h4>What APIs Consist of?</h4><p>As we are talking about testing APIs today, let me start with something that probably many of you already know, just to be on the same page.</p><p>Most commonly, applications with API on board consist of a server and some, as Aleksei likes to call them, “handles” “sticking out” of the server, that you could operate with. In reality, those are simply some endpoints with different HTTP methods. By calling those methods, — if everything is written correctly (and you use REST API) — you either take out information using a GET method or send information with POST (adding new information) and PUT (changing existing data). And to test these methods, you somehow need to access them — which is usually done by implementing a client.</p><h4>What is an API Client?</h4><p>A client is a proxy between your tests and your API, an application driver for your API, if you like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dslOy_cE-7cfJrz2H36koQ.png" /></figure><p>And many testers create API clients themselves.</p><p>API client usually consists of the following basic parts that are <em>universal</em>:</p><ol><li><strong>HTTP Client</strong>: it sends HTTP requests towards API;</li><li><strong>Configuration</strong>: it stores URLs, passwords, logins and other settings;</li><li><strong>Logs</strong>: will log anything you tell them to: usually what is sent to the server and server responses;</li></ol><p>And some <em>optional</em> ones that vary from one case to another:</p><p>4. <strong>Models</strong>: for example, if you have a user, you need to describe him, clarify what properties does he have and what he can do;</p><p>5.<strong> API methods</strong>: a wrapper around an endpoint that tells you how to access the HTTP server, for example, if you want to get some information about a logged-in user.</p><h4>Ways of Creating Clients: API Specification or Lack Thereof</h4><p>Sometimes testers have to write the client themselves. Usually, that happens when developers do not care about testing their code too much and do not provide proper API documentation.</p><p>When developers <em>do</em> care about their code being tested the best way possible, they create specifications for their API code in OpenAPI format — the most popular specification standard.</p><p>There are several tools to help you describe APIs by this standard. One of the most common ones is Swagger. Note: there is Swagger Specification and OpenAPI Specification, the latter being the former’s community fork.</p><p>What your API specification looks like when written by the OpenAPI standard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*61CavhBUsV2hKgD9AwNu0w.png" /></figure><p>Usually, this is written by developers — when they develop API, they document it at the same time. And this is how it can be done (I will also show you some examples later):</p><ol><li>Describe an endpoint;</li><li>Describe its methods;</li><li>Provide examples of responses that this method could return.</li></ol><p>It is easy to maintain and change. Having a specification, testers can use OpenAPI Generator and generate a client like the one we talked about before. You will get great results:</p><ol><li>A generated client will work no worse than a hand-written one;</li><li>OpenAPI Generator supports <a href="https://openapi-generator.tech/docs/generators#client-generators">many languages</a>;</li><li>You can specify what HTTP client to use;</li><li>The option of setting up other parameters.</li></ol><p>You will have everything you need for testing: HTTP Client, Config, Logs, Models, API Methods. All you need to do is to connect this client to your tests and start calling API methods. You only write tests and do not waste your time on any nonsense.</p><h4>Cons and Pros of Generated Clients</h4><p><em>Cons:</em></p><ul><li>Generated code is not very neat and is not maintainable. If you need to change something, you either need to change the specification or need to reload methods or classes provided by the generated client.</li></ul><p><em>Here is an example</em>. In one of our APIs, we have a rather unusual authorisation mechanism: the user needs to visit a web form first, authorise there and only then he can use the API. In testing, the first requests we have are for the web form and authorisation, where we get the cookies and save them. And only after that, we pass them to the API client. So here is the problem: for that, we need to inherit User API from the client-provided class and to add a login method.</p><p>Anyway, even with all that in mind, we saved a lot of time.</p><p><em>Pros:</em></p><ul><li>You have a working client ready for testing;</li><li>You always have a contract that your API uses. And if the contract changes (if developers changed something in the API), your tests will immediately start failing. This approach can help you monitor the changes constantly;</li><li>If you generate a client and some method or property is missing (developers forgot to add it), when you try to run your tests you will instantly see that, thus finding some bugs quicker;</li><li>This client can help with not only testing but also monitoring or creating other applications based on it.</li></ul><p>So, the pros significantly outweigh the cons.</p><h4>How to setup client</h4><ol><li><strong>Running a Server</strong></li></ol><p>Let us run an <a href="https://github.com/swagger-api/swagger-petstore">API application</a>:</p><pre>docker run — name swaggerapi-petstore2 -d -p 8080:8080 swaggerapi/petstore3:unstable java -jar -DswaggerUrl=openapi.yaml /swagger-petstore/jetty-runner.jar /swagger-petstore/server.war </pre><p>and check it:</p><p><a href="http://localhost:8080/">http://localhost:8080/</a></p><p>You will see a page with API documentation. Petstore API is a good example of OpenAPI using:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ugmliRNpBctYtdbMjP417g.png" /></figure><p>It gives examples for different methods — something to do with <em>pets</em>, <em>the store,</em> and with <em>the user</em>. You can already interact with different methods and find some bugs without any automation. Just press “Try it out” on any of the methods to send some requests and receive responses:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*vL5h9xrILklcDJGraAg6RA.png" /></figure><p>The specification itself looks a little scary — it is a huge JSON file, but in a month working with OpenAPI you will become an expert in it. Let us take a look:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*lAdnTfKkAzoMsyZ6GJ6xrQ.png" /></figure><p>It is based on the scheme we talked about previously. Here we have:</p><ul><li>An endpoint: /pet</li><li>A method: PUT</li><li>What <strong>request</strong> it receives and what <strong>response</strong> it returns.</li></ul><p>Not only can you generate a client, but also create a stub server that will be giving you back what is described in &quot;responses&quot; even if the application is not finished yet – so you could start designing your tests in advance.</p><p><strong>2. Generating a Client</strong></p><p>About the client generation: there are many ways you could do that. The one is to create a separate project, like “Your_App Contract”, where it will be built and where the client will be stored, with the extension corresponding to the language you have chosen.</p><p>If you have several clients, you can switch between different versions of these clients to make sure they are consistent.</p><p>Aleksei suggests using Docker to generate a java client :</p><pre>docker run — rm -v ${PWD}:/depot openapitools/openapi-generator-cli generate \ <br>-i depot/openapi.json \ <br>-g java \ <br>-o /depot/client/</pre><p>A few words about this command:</p><p>-i tells it where to get the JSON: you can either use URL or give a path to the file;</p><p>-g is a language you need to generate the client in;</p><p>-o is the location for the output.</p><p>This command will generate a client that should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1PLSCtAGV0fb574pch1aDg.png" /></figure><p>It already has different options: .pom for Maven, Gradle — you can choose any. Now you only need to follow instructions from the README: installation, adding the client to your project, and even an example of how to use it.</p><p>So, basically, it is down to:</p><p>Generating the client;</p><p>Installing it;</p><p>Connecting it to your project.</p><p><strong>3. Testing</strong></p><p>Let us look at some <a href="https://github.com/a-chumagin/openapi.client/tree/master/src/test/java/petstoreapi/tests">test examples</a> now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*sa8Nqv2QRbw7m8VSP_J6Fw.png" /></figure><p>So as you now have a client, you simply need to create an instance of a configurational class and put your settings into it. Had you written the client manually, you would have probably ended up with a similar result and used with the same approach:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*Jnt3XXZuFNxwXKxSV9_rhQ.png" /></figure><p>What is set up here:</p><ul><li>The basic URL;</li><li>A user, its password, etc.</li></ul><p>On line 26, we create an instance of a StoreApi (remember, we are testing a Petstore, and this API is responsible for methods related to the store) — and now we have a sort of a DSL that describes your application. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*TgLPKdi1arjDr88aK_8U2g.png" /></figure><p>On line 39, we have a “placeOrder” API instance. If you remember, in the API of the store we also have this POST method that places an order for a pet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cGde7J_ucZPemVYfzOt4gw.png" /></figure><p>If you need to generate an entity, you just need to create one, as the Order here, on line 35. You can also add different properties to it (such as id, pet id, etc).</p><p>You really do not need to be a coding superhero to write tests by adopting this approach. Now you can have tests running, even though you did not have to code the client.</p><p><strong>4. Postman</strong></p><p>But what if you do not want to automate anything or cannot write any code? For that, we have Postman.</p><p>In Postman, all you need to do is to import your JSON specification as a file or via a link to generate a collection:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*kF-0kdXl5aCrbMiavJOqfw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*6LjdVYmuuIyWxzHWgFuNvw.png" /></figure><p>And, as easy as that, Postman generated the methods for you to call:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*yNHDaDx-DQHTMCMp-2h98Q.png" /></figure><p>Here we get the same Store, Order, and the POST method that places an order for a pet. It even generated the variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*u3_or2I3nRwRHxet3seXug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/409/1*9uuVsBmuJwNGN5aVq5_zxA.png" /></figure><p>On the environment level, we have already defined the localhost:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*LW80MzW1CvSWvRwoyJfxWw.png" /></figure><p>And if we send a GET request that returns pet inventories by status, we will receive a response, the same one we have seen on the web interface of the Petstore:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*uORUlaSlg8s6VwKctMEOvQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*vHcs6oigzH0ESbN3tmZwFQ.png" /></figure><h4>In Conclusion</h4><p>I hope Aleksei Chumagin’s report will help you persuade your developers to use specifications to help you test more efficiently, or, maybe, even inspire you to learn how to write these specifications on your own. This will be highly appreciated!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73a00f59ca16" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>