<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rodolfo Marriel</title>
    <description>The latest articles on DEV Community by Rodolfo Marriel (@rod292).</description>
    <link>https://dev.to/rod292</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F314220%2F8961dc89-ddcf-4bdd-a64f-b8cba8fc05d0.jpeg</url>
      <title>DEV Community: Rodolfo Marriel</title>
      <link>https://dev.to/rod292</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rod292"/>
    <language>en</language>
    <item>
      <title>Introdução ao cucumber</title>
      <dc:creator>Rodolfo Marriel</dc:creator>
      <pubDate>Tue, 20 Dec 2022 21:49:41 +0000</pubDate>
      <link>https://dev.to/rod292/introducao-ao-cucumber-4jn8</link>
      <guid>https://dev.to/rod292/introducao-ao-cucumber-4jn8</guid>
      <description>&lt;p&gt;O Cucumber é uma ferramenta de teste de software que permite escrever cenários de teste em linguagem natural e, em seguida, verificar se o sistema está comportando-se de acordo com esses cenários. Esses cenários são escritos em um formato chamado Gherkin, que é uma linguagem específica para descrever cenários de teste. O Cucumber é amplamente utilizado em projetos de teste de aceitação, pois permite que os desenvolvedores, gerentes de projeto e outros partes interessadas escrevam cenários de teste de maneira clara e concisa, sem a necessidade de conhecimento técnico.&lt;/p&gt;

&lt;p&gt;Existem anotações pré definidas que são utilizados na hora de especificar cenários de teste: &lt;strong&gt;&lt;em&gt;Given&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;When&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;Then&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;And&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;But&lt;/em&gt;&lt;/strong&gt;. As anotações permitem que o compilador saiba o que deve ser feito na execução. Para ilustrar como o Cucumber funciona, vamos considerar um exemplo de um sistema de gerenciamento de livros. O Gherkin para este cenário poderia ser escrito da seguinte maneira:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="nl"&gt;Scenario&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;Request a book by id&lt;/span&gt;
    Given &lt;span class="nb"&gt;set &lt;/span&gt;book &lt;span class="nb"&gt;id &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;search 1
    When find a book by &lt;span class="nb"&gt;id&lt;/span&gt;
    Then the request status &lt;span class="k"&gt;for &lt;/span&gt;a book &lt;span class="nb"&gt;id &lt;/span&gt;mut &lt;span class="k"&gt;return &lt;/span&gt;200 HTTP status
    And the request must &lt;span class="k"&gt;return &lt;/span&gt;a valid book
    And &lt;span class="k"&gt;for &lt;/span&gt;book 1 title must be &lt;span class="s2"&gt;"Amsterdam"&lt;/span&gt; and author &lt;span class="s2"&gt;"Ian McEwan"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Este cenário descreve o comportamento esperado do sistema quando um livro é pesquisado. O Cucumber lê esse cenário e, em seguida, chama o código para verificar se o sistema está se comportando de acordo com ele. Se o sistema não estiver se comportando de acordo com o cenário, o Cucumber apresentará um erro.&lt;/p&gt;

&lt;p&gt;Para implementar este cenário de teste, seria necessário escrever código que verifique se o livro pesquisado realmente retornou como esperado. Um exemplo de código em Java seria:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Given&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"set book id for search {long}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setBookIdForSearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bookId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@When&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"find a book by id"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;findABookById&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;RestTemplate&lt;/span&gt; &lt;span class="n"&gt;restTemplate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RestTemplate&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;forEntity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;restTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getForEntity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ENDPOINT&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1L&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;bookResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;forEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBody&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;httpStatusResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;forEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getStatusCode&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Then&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"the request status for a book id mut return {int} HTTP status"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;theRequestStatusForABookIdMutReturnHTTPStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;httpStatusResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;equalTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@And&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"the request must return a valid book"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;theRequestMustReturnAValidBook&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;assertNotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookResponse&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;assertNotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;assertNotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getTitle&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;assertNotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAuthor&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@And&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"for book {long} title must be {string} and author {string}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;forBookTitleMustBeAndAuthor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;equalTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getTitle&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;equalTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bookResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAuthor&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;equalTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Este código implementa cada uma das etapas do cenário de teste em Gherkin.&lt;/p&gt;

&lt;p&gt;Um dos principais benefícios do Cucumber é que ele permite aos stakeholders escreverem cenários de teste sem conhecimento técnico, o que facilita a comunicação e a colaboração entre os membros do time. Além disso, o Cucumber é amplamente compatível com diversas linguagens de programação, o que significa que pode ser utilizado em praticamente qualquer projeto de software.&lt;/p&gt;

&lt;p&gt;Outro benefício do Cucumber é que ele permite que os cenários de teste sejam reutilizados. Isso significa que, uma vez que um cenário de teste foi escrito e implementado, ele pode ser facilmente reutilizado em diferentes projetos ou até mesmo em projetos futuros. Isso pode economizar muito tempo e esforço ao longo do tempo.&lt;/p&gt;

&lt;p&gt;Página documentação do cucumber: &lt;a href="https://cucumber.io/"&gt;https://cucumber.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Código fonte: &lt;a href="https://github.com/rodolfomarriel/cucumberexample"&gt;https://github.com/rodolfomarriel/cucumberexample&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>cucumber</category>
      <category>bdd</category>
      <category>teste</category>
    </item>
  </channel>
</rss>
