{"id":11057,"date":"2024-02-14T12:03:05","date_gmt":"2024-02-14T17:03:05","guid":{"rendered":"https:\/\/www.redline13.com\/blog\/?p=11057"},"modified":"2024-02-14T12:03:05","modified_gmt":"2024-02-14T17:03:05","slug":"functional-testing-with-k6","status":"publish","type":"post","link":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/","title":{"rendered":"Functional Testing with k6"},"content":{"rendered":"<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11058\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11057-1.png\" alt=\"Functional Testing with k6\" width=\"400\" height=\"300\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11057-1.png 400w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11057-1-300x225.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/strong><\/p>\n<p>In previous posts we have covered the ability to use k6 for scalable load testing, however k6 is also a suitable framework for creating <a href=\"https:\/\/en.wikipedia.org\/wiki\/Functional_testing\" target=\"_blank\" rel=\"noopener\">functional tests<\/a>. In this brief post, we will show you how to get started with using k6 as a functional testing tool.<\/p>\n<h3><a id=\"post-11057-_viucvnu8jduh\"><\/a>Assertions in k6<\/h3>\n<p>There are two basic ways to write assertions in k6. One is using the built-in <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/javascript-api\/k6\/check\/\" target=\"_blank\" rel=\"noopener\">check<\/a> and <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/javascript-api\/k6\/group\/\" target=\"_blank\" rel=\"noopener\">group<\/a> functions. Perhaps a better alternative is to use the <a href=\"https:\/\/www.chaijs.com\/\" target=\"_blank\" rel=\"noopener\">ChaiJS<\/a> extension (<a href=\"https:\/\/k6.io\/docs\/javascript-api\/jslib\/k6chaijs\/\" target=\"_blank\" rel=\"noopener\">k6chaijs<\/a>), which is actually the favored approach in the <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/examples\/functional-testing\/\" target=\"_blank\" rel=\"noopener\">official k6 documentation<\/a>. The latter allows use of the <code>expect<\/code> function making functional tests more human-readable.<\/p>\n<h3><a id=\"post-11057-_vxdswunk1afl\"><\/a>Creating a k6 Functional Test<\/h3>\n<p>We will need to import the <a href=\"https:\/\/k6.io\/docs\/javascript-api\/jslib\/k6chaijs\/\" target=\"_blank\" rel=\"noopener\"><strong><code>k6chaijs<\/code><\/strong><\/a> extension to allow us to use the <code>describe<\/code> and <code>expect<\/code> methods. Add the following line to the top of your k6 test plan script:<\/p>\n<pre><strong>import chai, { describe, expect } from 'https:\/\/jslib.k6.io\/k6chaijs\/4.3.4.3\/index.js';<\/strong><\/pre>\n<p>Using the <em>describe-expect<\/em> pattern, we can write functional statements such as the following:<\/p>\n<pre><strong>describe('Hello world!', () =&gt; {\n<\/strong><strong>\u00a0 \u00a0 const response = http.get('https:\/\/test-api.k6.io\/');\n<\/strong><strong>\u00a0 \u00a0 expect(response.status, 'response status').to.equal(200);\n<\/strong><strong>});<\/strong><\/pre>\n<p>In this example, a successful response from a request to the <a href=\"https:\/\/test-api.k6.io\/\" target=\"_blank\" rel=\"noopener\">k6 test-api<\/a> service is asserted. As long as the request out to this endpoint returns with a <code>200<\/code> success response, the test will continue to the next statement.<\/p>\n<h3><a id=\"post-11057-_bgck1ji2u5m5\"><\/a>Advanced Functionality<\/h3>\n<p>The <a href=\"https:\/\/www.chaijs.com\/api\/bdd\/\" target=\"_blank\" rel=\"noopener\">ChaiJS assertion library<\/a> extends a suite of getter functions that can be chained together in complex ways. The above example shows the simplest assertion which chains the <code>to<\/code> and <code>equal<\/code>\u00a0getters to create an equivalence test. Let\u2019s consider the following assertion:<\/p>\n<pre><strong>expect(response.json()).to.have.a.property('items');<\/strong><\/pre>\n<p>Here the response object from a request is analyzed as <a href=\"https:\/\/en.wikipedia.org\/wiki\/JSON\" target=\"_blank\" rel=\"noopener\">JSON<\/a>, and evaluated to contain an \u201citems\u201d property. Only if the response contains such a named property will the assertion succeed.<\/p>\n<p>A few selected additional chained getters for assertions:<\/p>\n<ul>\n<li><code>.to.have.validJsonBody()<\/code> \u2192 stipulates that a valid JSON body must exist<\/li>\n<li><code>.to.be.a('string')<\/code> \u2192 analyzes the data type which must be a string<\/li>\n<li><code>.to.not.be.empty<\/code> \u2192 there must be a value specified for the analyzed target<\/li>\n<\/ul>\n<p>There is an example in the k6 documentation that <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/examples\/functional-testing\/#full-example-showcasing-all-functionality\" target=\"_blank\" rel=\"noopener\">showcases all functionality<\/a> of the ChaiJS describe-expect pattern. Furthermore, the <a href=\"https:\/\/www.chaijs.com\/api\/\">official ChaiJS documentation<\/a> contains detailed descriptions and additional examples for all chainable getters.<\/p>\n<hr \/>\n<p>Did you know that RedLine13 offers a full-featured, time-limited free trial? \u00a0<a href=\"https:\/\/www.redline13.com\/Service\" target=\"_blank\" rel=\"noopener\">Sign up now<\/a>, and move your k6 tests into the cloud today!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous posts we have covered the ability to use k6 for scalable load testing, however k6 is also a suitable framework for creating functional tests. In this brief post, we will show you how to get started with using k6 as a functional testing tool. Assertions in k6 There are two basic ways to write assertions in k6. One is using the built-in check and group functions. Perhaps a better alternative is to use the<a class=\"more-link\" href=\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":11,"featured_media":11058,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[608,596,693,679,692,671,677,318,424],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-dkoziel","4":"post-11057","6":"format-standard","7":"has-post-thumbnail","8":"category-blog","9":"category-uncategorized","10":"post_tag-functional-testing","11":"post_tag-k6","12":"post_tag-k6-assertions","13":"post_tag-k6-extensions","14":"post_tag-k6-functional-test","15":"post_tag-k6-load-tests","16":"post_tag-k6-script","17":"post_tag-load-testing","18":"post_tag-redline13"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Functional Testing with k6 - RedLine13<\/title>\n<meta name=\"description\" content=\"Previously we have covered load testing with k6. In this brief post, we will illustrate how to use k6 as a functional testing tool.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Functional Testing with k6 - RedLine13\" \/>\n<meta property=\"og:description\" content=\"Previously we have covered load testing with k6. In this brief post, we will illustrate how to use k6 as a functional testing tool.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\" \/>\n<meta property=\"og:site_name\" content=\"RedLine13\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-14T17:03:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11057-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"David Koziel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"David Koziel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\"},\"author\":{\"name\":\"David Koziel\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\"},\"headline\":\"Functional Testing with k6\",\"datePublished\":\"2024-02-14T17:03:05+00:00\",\"dateModified\":\"2024-02-14T17:03:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\"},\"wordCount\":355,\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"keywords\":[\"Functional Testing\",\"k6\",\"k6 assertions\",\"k6 extensions\",\"k6 functional test\",\"k6 load tests\",\"k6 script\",\"Load Testing\",\"RedLine13\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\",\"name\":\"Functional Testing with k6 - RedLine13\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\"},\"datePublished\":\"2024-02-14T17:03:05+00:00\",\"dateModified\":\"2024-02-14T17:03:05+00:00\",\"description\":\"Previously we have covered load testing with k6. In this brief post, we will illustrate how to use k6 as a functional testing tool.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.redline13.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Functional Testing with k6\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"name\":\"RedLine13\",\"description\":\"(Almost) Free Load Testing in the Cloud\",\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.redline13.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\",\"name\":\"RedLine13\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"contentUrl\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"width\":300,\"height\":68,\"caption\":\"RedLine13\"},\"image\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\",\"name\":\"David Koziel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"caption\":\"David Koziel\"},\"url\":\"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Functional Testing with k6 - RedLine13","description":"Previously we have covered load testing with k6. In this brief post, we will illustrate how to use k6 as a functional testing tool.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/","og_locale":"en_US","og_type":"article","og_title":"Functional Testing with k6 - RedLine13","og_description":"Previously we have covered load testing with k6. In this brief post, we will illustrate how to use k6 as a functional testing tool.","og_url":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/","og_site_name":"RedLine13","article_published_time":"2024-02-14T17:03:05+00:00","og_image":[{"width":400,"height":300,"url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/02\/word-image-11057-1.png","type":"image\/png"}],"author":"David Koziel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"David Koziel","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/#article","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/"},"author":{"name":"David Koziel","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20"},"headline":"Functional Testing with k6","datePublished":"2024-02-14T17:03:05+00:00","dateModified":"2024-02-14T17:03:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/"},"wordCount":355,"publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"keywords":["Functional Testing","k6","k6 assertions","k6 extensions","k6 functional test","k6 load tests","k6 script","Load Testing","RedLine13"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/","url":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/","name":"Functional Testing with k6 - RedLine13","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/#website"},"datePublished":"2024-02-14T17:03:05+00:00","dateModified":"2024-02-14T17:03:05+00:00","description":"Previously we have covered load testing with k6. In this brief post, we will illustrate how to use k6 as a functional testing tool.","breadcrumb":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.redline13.com\/blog\/2024\/02\/functional-testing-with-k6\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.redline13.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Functional Testing with k6"}]},{"@type":"WebSite","@id":"https:\/\/www.redline13.com\/blog\/#website","url":"https:\/\/www.redline13.com\/blog\/","name":"RedLine13","description":"(Almost) Free Load Testing in the Cloud","publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.redline13.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.redline13.com\/blog\/#organization","name":"RedLine13","url":"https:\/\/www.redline13.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","contentUrl":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","width":300,"height":68,"caption":"RedLine13"},"image":{"@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20","name":"David Koziel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","caption":"David Koziel"},"url":"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/comments?post=11057"}],"version-history":[{"count":4,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11057\/revisions"}],"predecessor-version":[{"id":11062,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11057\/revisions\/11062"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media\/11058"}],"wp:attachment":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media?parent=11057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/categories?post=11057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/tags?post=11057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}