{"id":9758,"date":"2022-11-20T16:39:38","date_gmt":"2022-11-20T16:39:38","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9758"},"modified":"2022-11-20T16:39:38","modified_gmt":"2022-11-20T16:39:38","slug":"swift-unit-tests-including-and-using-json-files","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/swift-unit-tests-including-and-using-json-files\/","title":{"rendered":"Swift unit tests, including and using .json files"},"content":{"rendered":"<p><em>Note: I&#8217;m running everything on Ubuntu 20.0.4 using Swift version 5.7.1. I&#8217;m assuming everything listed below works on Mac etc. as well.<\/em><\/p>\n<p>I have a simple Eureka client package (named SwiftEureka) that I&#8217;ve been working on. Now I&#8217;ve tested it against a running instance of Eureka but I want to write some unit tests where I don&#8217;t need the server running. The idea simply being that I have a bunch of .json files which contain the JSON responses, taken from Eureka, for various REST calls.<\/p>\n<p>So, to summarise, I basically want to have .json files within my Tests folder and use them within my unit tests. <\/p>\n<p>Let&#8217;s assume we have our Tests\/EurekaTests folder and within that we have a file named applications.json. We need to add this to the Package.swift file under the .testTarget section, like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n.testTarget(\r\n   name: &quot;EurekaTests&quot;,\r\n   dependencies: &#x5B;&quot;SwiftEureka&quot;],\r\n      resources: &#x5B;\r\n         .process(&quot;applications.json&quot;)\r\n      ]),\r\n<\/pre>\n<p>Now the file will be seen by Swift as part of the package&#8217;s bundle.<\/p>\n<p>Next, we need to load the file into our tests. We do this by using the <em>Bundle<\/em> object, like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nguard let path = Bundle.module.url(forResource: &quot;applications&quot;, withExtension: &quot;json&quot;) else {\r\n   XCTFail(&quot;Missing file: applications.json&quot;)\r\n   return\r\n}\r\n\r\nlet json = try Data(contentsOf: path)\r\nlet wrapper = try JSONDecoder().decode(ApplicationsWrapper.self, from: json)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note: I&#8217;m running everything on Ubuntu 20.0.4 using Swift version 5.7.1. I&#8217;m assuming everything listed below works on Mac etc. as well. I have a simple Eureka client package (named SwiftEureka) that I&#8217;ve been working on. Now I&#8217;ve tested it against a running instance of Eureka but I want to write some unit tests where [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9758","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9758"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9758\/revisions"}],"predecessor-version":[{"id":9761,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9758\/revisions\/9761"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}