{"id":9129,"date":"2022-10-10T20:43:33","date_gmt":"2022-10-10T20:43:33","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9129"},"modified":"2022-10-10T20:43:33","modified_gmt":"2022-10-10T20:43:33","slug":"unit-testing-with-swift-2","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/unit-testing-with-swift-2\/","title":{"rendered":"Unit testing with Swift"},"content":{"rendered":"<p>Unit test classes in Swift derive from <em>XCTestCase<\/em>. You&#8217;ll need to import <em>XCTest<\/em>. <\/p>\n<p>So for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimport XCTest\r\n\r\nclass MvvmTestTests: XCTestCase {\r\n}\r\n<\/pre>\n<p>Test functions need to be prefixed with <em\\>test<\/em> so for example, if we assume we have a global function like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nfunc add(_ a: Int, _ b: Int) -&gt; Int {\r\n    return a + b\r\n}\r\n<\/pre>\n<p>then we might have a test like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nfunc testAdd() throws {\r\n    XCTAssertEqual(5, MyApp.add(2, 3))\r\n}\r\n<\/pre>\n<p>Before we can test our <em>add<\/em> function we need to make the application code available to the test code, to do this after the <em>import XCTest<\/em> line add<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@testable import MyApp\r\n<\/pre>\n<p>This actually allows our tests to access our code without having to mark all the code as public.<\/p>\n<p>As you&#8217;ve seen we use <em>XCTAssertEqual<\/em> to assert our expectations against actual.<\/p>\n<p><strong>Measuring Performance<\/strong><\/p>\n<p>We can wrap our code, within a test, using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nself.measure {\r\n}\r\n<\/pre>\n<p>So for example (whilst a little pointless on our add function) we can measure performance like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nself.measure {\r\n   _  = MvvmTest.add(2, 3)\r\n}\r\n<\/pre>\n<p>We can also apply options to the <em>measure<\/em> function<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nlet option = XCTMeasureOptions()\r\noption.iterationCount = 100\r\nself.measure(options: option) {\r\n    _  = MvvmTest.add(2, 3)\r\n}\r\n<\/pre>\n<p>In this case we&#8217;ll run the measure block 100 + 1 times. The iteration actually ignores the first run (hence the + 1) this tries to remove cold start times.<\/p>\n<p><strong>Running our tests<\/strong><\/p>\n<p>In Xcode, select the Test Navigator, this should show you all the tests found. Simply select either a single test or group of tests and right mouse click. Select the Run option and your tests will run. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit test classes in Swift derive from XCTestCase. You&#8217;ll need to import XCTest. So for example import XCTest class MvvmTestTests: XCTestCase { } Test functions need to be prefixed with test so for example, if we assume we have a global function like this func add(_ a: Int, _ b: Int) -&gt; Int { return [&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-9129","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\/9129","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=9129"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9129\/revisions"}],"predecessor-version":[{"id":9130,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9129\/revisions\/9130"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}