{"id":9325,"date":"2022-10-09T11:36:38","date_gmt":"2022-10-09T11:36:38","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9325"},"modified":"2022-10-09T11:36:38","modified_gmt":"2022-10-09T11:36:38","slug":"unit-testing-with-swift","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/unit-testing-with-swift\/","title":{"rendered":"Unit testing with Swift"},"content":{"rendered":"<p>Swift comes with its own unit testing framework <a href=\"https:\/\/developer.apple.com\/documentation\/xctest\" rel=\"noopener\" target=\"_blank\">XCTest<\/a>, let&#8217;s take a look at what we need to do to enable unit testing and how we use this library.<\/p>\n<p><strong>Test Package Dependencies<\/strong><\/p>\n<p>If you created your package via the Wwift package manager your project will be laid out with <em>Sources<\/em> and <em>Tests<\/em> folders. <\/p>\n<p>Here&#8217;s an example of a library package, notice the <em>.testTarget<\/em> section<\/p>\n<pre class=\"brush: java; highlight: [5,6,7]; title: ; notranslate\" title=\"\">\r\ntargets: &#x5B;\r\n   .target(\r\n      name: &quot;MyLib&quot;,\r\n      dependencies: &#x5B;]),\r\n   .testTarget(\r\n      name: &quot;MyLibTests&quot;,\r\n      dependencies: &#x5B;&quot;MyLib&quot;]),\r\n]\r\n<\/pre>\n<p><strong>Creating a test fixture<\/strong><\/p>\n<p>Next up we need to import the XCTest library and derive our test fixture\/class from <em>XCTestCase<\/em>, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfinal class MyLibTests: XCTestCase {\r\n   \/\/ tests go here\r\n}\r\n<\/pre>\n<p><strong>Creating a test<\/strong><\/p>\n<p>Tests are written in test methods within the test case subclass. Test methods have no parameters, no return value and the name should begin with lowercase prefix <em>test<\/em>, so for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfinal class MyLibTests: XCTestCase {\r\n   func testIsValid() {\r\n     \/\/ test setup and asserts\r\n   }\r\n}\r\n<\/pre>\n<p><strong>Asserting our tests<\/strong><\/p>\n<p>The XCTest library prefixes its assertion method with XCT. Hence, we have <em>XCTAssertEqual<\/em> for example to assert that two values are equal<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfinal class MyLibTests: XCTestCase {\r\n   func testIsValid() {\r\n      let a = 1\r\n      let b = 1\r\n      XCTAssertEqual(a, b)\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Swift comes with its own unit testing framework XCTest, let&#8217;s take a look at what we need to do to enable unit testing and how we use this library. Test Package Dependencies If you created your package via the Wwift package manager your project will be laid out with Sources and Tests folders. Here&#8217;s an [&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,185],"tags":[],"class_list":["post-9325","post","type-post","status-publish","format-standard","hentry","category-swift","category-unit-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9325","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=9325"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9325\/revisions"}],"predecessor-version":[{"id":9492,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9325\/revisions\/9492"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}