{"id":64704,"date":"2024-08-30T04:31:41","date_gmt":"2024-08-29T23:01:41","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=64704"},"modified":"2024-09-04T15:15:21","modified_gmt":"2024-09-04T09:45:21","slug":"swifts-new-testing-framework","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/swifts-new-testing-framework\/","title":{"rendered":"Swift\u2019s New Testing Framework"},"content":{"rendered":"<p>WWDC 2024 unveiled a major improvement to XCTest, which is <strong>Swift&#8217;s Testing Framework<\/strong>. I found it really beneficial while working on this. So I decided to share it with all of you. Let&#8217;s begin.<\/p>\n<h2><strong>Introduction<\/strong><\/h2>\n<p>Swift Testing allows you to use the Swift programming language\u2019s powerful and expressive capabilities to write tests with greater confidence and less code. The library works smoothly with Swift Package Manager and Xcode\u2019s testing workflow, allowing for flexible test organization, customizable metadata, and scalable test execution<\/p>\n<h2><strong>XCTest Vs Swift\u00a0Testing<\/strong><\/h2>\n<p>Now it\u2019s time to figure out why we require the Swift Testing Framework when we already have the XCTest Framework. How does it vary from XCTest, and what are the advantages of utilizing it over XCTest?<\/p>\n<h2><strong>XCTest Framework<\/strong><\/h2>\n<p>XCTest is Apple\u2019s default testing framework for Swift and Objective-C. It is built into Xcode and offers full support for unit, performance, and UI testing. XCTest is widely used because of its smooth interaction with Xcode, making it the preferred choice for many Swift developers.<\/p>\n<h2><strong>Key Features<\/strong><\/h2>\n<ol>\n<li><strong>Integration with Xcode:<\/strong> XCTest is strongly linked with Xcode, allowing for the simple creation of tests, execution, and debugging.<\/li>\n<li><strong>Assertion Library:<\/strong> XCTest provides a variety of assertions to validate test scenarios. Which makes it simple to utilize.<\/li>\n<li><strong>Code Coverage:<\/strong> Supports code coverage analysis to discover untested code.<\/li>\n<\/ol>\n<h3>Example:<\/h3>\n<div id=\"attachment_64705\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64705\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64705\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-12.15.29\u202fPM-1024x417.png\" alt=\"XCTest Example\" width=\"625\" height=\"255\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-12.15.29\u202fPM-1024x417.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-12.15.29\u202fPM-300x122.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-12.15.29\u202fPM-768x313.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-12.15.29\u202fPM-1536x626.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-12.15.29\u202fPM-624x254.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-12.15.29\u202fPM.png 1568w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64705\" class=\"wp-caption-text\">XCTest Example<\/p><\/div>\n<h2>Swift Testing Framework<\/h2>\n<p>Swift Testing Framework, also known as Swift Test, is a modern framework created exclusively for writing tests in Swift. It seeks to give a more Swift-like, expressive, and powerful method of writing tests than the existing XCTest framework.<\/p>\n<h2>Key Features<\/h2>\n<ol>\n<li><strong>DSL for Tests:<\/strong> Swift Testing Frameworks frequently employ a Domain-Specific Language (DSL) to make test creation more intuitive and readable. This can result in tests that appear more like normal language than code.<\/li>\n<li><strong>First-Class Swift Support:<\/strong> These frameworks, which are specifically created for Swift, take advantage of the language\u2019s characteristics like optional, generics, and type inference to deliver a smoother testing experience.<\/li>\n<li><strong>Mocking and Stubbing:<\/strong> Many modern Swift Testing Frameworks provide support for mocking and stubbing, making it easy to separate the code being tested.<\/li>\n<li><strong>Asynchronous Testing:<\/strong> Improved support for testing asynchronous code, which is increasingly crucial in modern Swift applications.<\/li>\n<\/ol>\n<h2>Benefits of Swift\u00a0Testing<\/h2>\n<ol>\n<li><strong>Early Bug Detection:<\/strong> Writing tests helps detect bugs and obstacles early in the development process, minimizing the cost and work required to repair them later.<\/li>\n<li><strong>Refactoring Confidence:<\/strong> Tests serve as a safety net while rewriting code, ensuring that modifications do not introduce new bugs.<\/li>\n<li><strong>Documentation:<\/strong> Tests serve as documentation, demonstrating how the code should react in different contexts.<\/li>\n<li><strong>Continuous Integration:<\/strong> Automated tests can be added to continuous integration (CI) pipelines, guaranteeing that code changes are tested automatically.<\/li>\n<\/ol>\n<h2>Add Swift\u00a0Testing<\/h2>\n<p>Let\u2019s understand how to add Swift Testing to an Existing Project or a new one.<\/p>\n<p><strong>1. Adding In Existing Project:-<\/strong> To add a new target follow the steps.<br \/>\n<strong>Click File -&gt; New -&gt; Target<\/strong><\/p>\n<div id=\"attachment_64706\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64706\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64706\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.21.13\u202fPM-899x1024.png\" alt=\"Adding Target\" width=\"625\" height=\"712\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.21.13\u202fPM-899x1024.png 899w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.21.13\u202fPM-263x300.png 263w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.21.13\u202fPM-768x875.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.21.13\u202fPM-624x711.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.21.13\u202fPM.png 1192w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64706\" class=\"wp-caption-text\">Adding Target<\/p><\/div>\n<p><strong>2. Choose Target:-<\/strong> In the target popup search for unit testing and select it as shown in the image.<\/p>\n<div id=\"attachment_64707\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64707\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64707\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.23.59\u202fPM-1024x731.png\" alt=\"Unit Testing Target\" width=\"625\" height=\"446\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.23.59\u202fPM-1024x731.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.23.59\u202fPM-300x214.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.23.59\u202fPM-768x548.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.23.59\u202fPM-624x445.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.23.59\u202fPM.png 1466w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64707\" class=\"wp-caption-text\">Unit Testing Target<\/p><\/div>\n<p><strong>3. Target Name:-<\/strong> Now give the name to the target and under the testing system select Swift Testing and click on finish.<\/p>\n<div id=\"attachment_64708\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64708\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64708\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.25.26\u202fPM-1024x730.png\" alt=\"Give Name and Select Swift Testing\" width=\"625\" height=\"446\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.25.26\u202fPM-1024x730.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.25.26\u202fPM-300x214.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.25.26\u202fPM-768x547.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.25.26\u202fPM-624x445.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-3.25.26\u202fPM.png 1468w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64708\" class=\"wp-caption-text\">Give Name and Select Swift Testing<\/p><\/div>\n<p>Now we have imported Test Target here and It\u2019s time to write our first swift Test case.<\/p>\n<h2>Building Block of Swift\u00a0Testing<\/h2>\n<h2><strong>1. Add Test\u00a0Case:<\/strong><\/h2>\n<div id=\"attachment_64709\" style=\"width: 870px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64709\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-64709 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-4.15.42\u202fPM.png\" alt=\"First Swift Example\" width=\"860\" height=\"400\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-4.15.42\u202fPM.png 860w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-4.15.42\u202fPM-300x140.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-4.15.42\u202fPM-768x357.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-4.15.42\u202fPM-624x290.png 624w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><p id=\"caption-attachment-64709\" class=\"wp-caption-text\">First Swift Example<\/p><\/div>\n<p>As per the above example, We just need to <strong>import the Testing<\/strong> framework to start working with <strong>Swift Testing<\/strong>. To make any function Testable we just need to add the <strong>@Test<\/strong> attribute. After adding this attribute a diamond box was added in front to test the function.<\/p>\n<h2><strong>2. Adding Expectation<\/strong><\/h2>\n<p>After adding the Test case we have to put our expectations by using macros provided by the framework.<\/p>\n<p><strong>i. #expect:-<\/strong> By using this macro we can define our expectations. It\u2019s validated whether the given condition is true or false. We don\u2019t need to add multiple asserts to validate Bool conditions like XCAssertEqual, XCTAssertNotEqual, XCTAssertTrue, etc. Let\u2019s understand it by example.<\/p>\n<div id=\"attachment_64712\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64712\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64712\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-5.56.11\u202fPM-1024x501.png\" alt=\"#expect macro uses\" width=\"625\" height=\"306\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-5.56.11\u202fPM-1024x501.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-5.56.11\u202fPM-300x147.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-5.56.11\u202fPM-768x376.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-5.56.11\u202fPM-1536x752.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-5.56.11\u202fPM-624x305.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-19-at-5.56.11\u202fPM.png 1958w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64712\" class=\"wp-caption-text\">#expect macro uses<\/p><\/div>\n<p>As we can see one <strong>success<\/strong> expectation and one <strong>failure<\/strong> expectation. Here we are Testing two cases one is equalTo and the other is greaterThan and both can be executed using <strong>#expect<\/strong> macro. While in <strong>XCTest<\/strong> we have two asserts in this case.<\/p>\n<p><strong>ii: #expect(.throws: (any Error).self):-<\/strong> This macro is used to expect an error to be thrown. You specify the error type or the actual error that you expect to be thrown. The test will fail if the code within the macro does not throw an error, or if the error is not what is intended. By using <strong>(any Error).self<\/strong>, you are indicating that the code should throw any error that confirms the Error protocol.<\/p>\n<div id=\"attachment_64682\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64682\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64682\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-9.40.22\u202fPM-1024x535.png\" alt=\"Example of #expect(.throws: (any Error).self)\" width=\"625\" height=\"327\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-9.40.22\u202fPM-1024x535.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-9.40.22\u202fPM-300x157.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-9.40.22\u202fPM-768x401.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-9.40.22\u202fPM-1536x803.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-9.40.22\u202fPM-624x326.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-9.40.22\u202fPM.png 1932w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64682\" class=\"wp-caption-text\">Example of #expect (.throws: (any Error).self)<span style=\"color: #444444; font-size: 16px;\">.<\/span><\/p><\/div>\n<p><strong>iii. #require macro:-<\/strong> This macro is used, as its name suggests when a method needs to run certain prerequisites before continuing with the test case. If the necessary circumstances are not met, the test fails. Let us examine an example.<\/p>\n<div id=\"attachment_64683\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64683\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64683\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.09.02\u202fPM-1024x231.png\" alt=\"Example of #require macro\" width=\"625\" height=\"141\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.09.02\u202fPM-1024x231.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.09.02\u202fPM-300x68.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.09.02\u202fPM-768x173.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.09.02\u202fPM-1536x346.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.09.02\u202fPM-624x141.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.09.02\u202fPM.png 1942w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64683\" class=\"wp-caption-text\">Example of #require macro<\/p><\/div>\n<p>In the above example, we can see that if a name is nil <strong>#require<\/strong> macros get failed and stop the execution of <strong>#expect<\/strong> macro.<\/p>\n<p><strong>Swift\u2019s New Testing<\/strong> Framework has added a number of new macros. In upcoming articles, we shall talk more about that. Let us now go to another set of building blocks.<\/p>\n<h2><strong>3. Traits<\/strong><\/h2>\n<p>Traits are basically to used for defining the characteristics, attributes, or behaviors of a block of code or function. It is also used to define the descriptive information of test cases. Let\u2019s have a look at how to define a trait.<\/p>\n<div id=\"attachment_64684\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64684\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64684\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.29.53\u202fPM-1024x534.png\" alt=\"Example of Traits\" width=\"625\" height=\"326\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.29.53\u202fPM-1024x534.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.29.53\u202fPM-300x156.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.29.53\u202fPM-768x400.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.29.53\u202fPM-1536x801.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.29.53\u202fPM-2048x1067.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-10.29.53\u202fPM-624x325.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64684\" class=\"wp-caption-text\">Example of Traits<\/p><\/div>\n<p>As we can see in the above example when we define the traits for any test case method it replaces the name of the function with the given traits in the Left Navigation Pane. It is useful to define the cases and easy to find out and debug when it fails.<\/p>\n<p>There are so many Traits that are introduced with <strong>Swift\u2019s New Testing<\/strong> Framework. You can check in this <a href=\"https:\/\/developer.apple.com\/documentation\/testing\/trait\">Link.<\/a><\/p>\n<h2><strong>4. Test\u00a0Suites<\/strong><\/h2>\n<p>Suites are basically used to combine the <strong>Test function<\/strong> and <strong>Suites<\/strong>. It can be annotated using <strong>@suite<\/strong>. Any <strong>Test function<\/strong> is implicitly considered a suite. Suites can have stored properties. It can have <strong>init<\/strong> and <strong>deInit<\/strong> methods for <strong>set-up<\/strong> and <strong>tear-down<\/strong> logic respectively. Whenever we create an object of any suite it automatically calls <strong>init()<\/strong> method and after Test cases run successfully it needs to be de-initialized like we were working on class and structs.\u00a0 Let\u2019s have a look at an example.<\/p>\n<div id=\"attachment_64687\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64687\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64687\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.07.04\u202fPM-1024x372.png\" alt=\"Example of Suite\" width=\"625\" height=\"227\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.07.04\u202fPM-1024x372.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.07.04\u202fPM-300x109.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.07.04\u202fPM-768x279.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.07.04\u202fPM-1536x559.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.07.04\u202fPM-2048x745.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.07.04\u202fPM-624x227.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64687\" class=\"wp-caption-text\">Example of Suite<\/p><\/div>\n<p>As we can see in the above example, All the test cases regarding Name are combined in a single suite and we have given descriptive info about the suite that will display in the left navigation pan as in the image.<\/p>\n<h1>Parameterized testing<\/h1>\n<p>Swift Testing framework allows us to pass parameters in a function that makes them testable with multiple different values. This makes tests more efficient and manageable. Let\u2019s look an example.<\/p>\n<div id=\"attachment_64688\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64688\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64688\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.18.33\u202fPM-1024x166.png\" alt=\"Example of Parameterized Test Case\" width=\"625\" height=\"101\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.18.33\u202fPM-1024x166.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.18.33\u202fPM-300x49.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.18.33\u202fPM-768x125.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.18.33\u202fPM-1536x250.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.18.33\u202fPM-624x101.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.18.33\u202fPM.png 1550w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64688\" class=\"wp-caption-text\">Example of Parameterized Test Case<\/p><\/div>\n<p>We have created a test case that expects that the given email ID should not be nil. But at the same time, we got a compile time error which indicates that we have to pass the argument value within the <strong>@Test Attributes<\/strong>. Let\u2019s correct it and see how it works.<\/p>\n<div id=\"attachment_64689\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64689\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64689\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.25.16\u202fPM-1024x283.png\" alt=\"Example of Test case with parameter\" width=\"625\" height=\"173\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.25.16\u202fPM-1024x283.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.25.16\u202fPM-300x83.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.25.16\u202fPM-768x213.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.25.16\u202fPM-1536x425.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.25.16\u202fPM-2048x567.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-27-at-11.25.16\u202fPM-624x173.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64689\" class=\"wp-caption-text\">Example of a Test case with a parameter<\/p><\/div>\n<p>As we can see in the given example, We are passing an <strong>array of strings<\/strong> and the <strong>Test function<\/strong> iterates each of them and validates them. It will display the result of each parameter whether that <strong>Passes<\/strong> or <strong>Fail<\/strong> and if fail then what will be the reason? We can also check the result in the <strong>Left Navigation Pan<\/strong>. We can re-execute only the failed argument.<\/p>\n<h2><strong>Conditional Testing<\/strong><\/h2>\n<p>While we are working big project that has several Test cases and is in maintenance mode, we get a task to change in existing behavior of the app. In this case, chances are very high to impact the test case. So We may be required to disable some test cases or only need to run in certain conditions. Here <strong>Swift Testing frameworks<\/strong> to help us to do this with minimal changes by using\u00a0<strong>.enable<\/strong> or\u00a0<strong>.disable<\/strong> attributes with <strong>@Test<\/strong>.<\/p>\n<div id=\"attachment_64680\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64680\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64680\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.35.07\u202fAM-1024x229.png\" alt=\"Example of\u00a0.enable attribute\" width=\"625\" height=\"140\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.35.07\u202fAM-1024x229.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.35.07\u202fAM-300x67.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.35.07\u202fAM-768x171.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.35.07\u202fAM-1536x343.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.35.07\u202fAM-624x139.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.35.07\u202fAM.png 1568w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64680\" class=\"wp-caption-text\">Example of\u00a0.enable attribute<\/p><\/div>\n<div id=\"attachment_64679\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-64679\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-64679\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.34.54\u202fAM-1024x145.png\" alt=\"Example of\u00a0.disable attribute\" width=\"625\" height=\"89\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.34.54\u202fAM-1024x145.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.34.54\u202fAM-300x43.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.34.54\u202fAM-768x109.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.34.54\u202fAM-1536x218.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.34.54\u202fAM-624x88.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-2024-08-28-at-10.34.54\u202fAM.png 1552w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-64679\" class=\"wp-caption-text\">Example of\u00a0.disable attribute<\/p><\/div>\n<p>In the given example we have used .enable and .disable attributes. For using .enable attribute we need to pass a specific condition. If the condition is true it will execute else it will skip the test. Same with the disable attribute. If we add this attribute, the test case will skipped and show the description.<\/p>\n<h2>Conclusion<\/h2>\n<p>Test behaviors can be declared with less code thanks to Swift Testing\u2019s expressive API with macros. Swift expressions and operators are used by the #expect API to validate the supplied expressions. Moreover, Swift Testing facilitates parallel testing, which raises the general productivity and efficiency of testing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WWDC 2024 unveiled a major improvement to XCTest, which is Swift&#8217;s Testing Framework. I found it really beneficial while working on this. So I decided to share it with all of you. Let&#8217;s begin. Introduction Swift Testing allows you to use the Swift programming language\u2019s powerful and expressive capabilities to write tests with greater confidence [&hellip;]<\/p>\n","protected":false},"author":1759,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":70},"categories":[1400],"tags":[6378],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/64704"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1759"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=64704"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/64704\/revisions"}],"predecessor-version":[{"id":65170,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/64704\/revisions\/65170"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=64704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=64704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=64704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}