{"id":33080,"date":"2021-07-08T12:06:33","date_gmt":"2021-07-08T16:06:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=33080"},"modified":"2021-07-08T12:06:33","modified_gmt":"2021-07-08T16:06:33","slug":"generic-test-classes-in-xcode-12-5","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/07\/08\/generic-test-classes-in-xcode-12-5\/","title":{"rendered":"Generic Test Classes in Xcode 12.5"},"content":{"rendered":"<p><a href=\"https:\/\/qualitycoding.org\/wwdc21-unit-testing\/\">Jon Reid<\/a>:<\/p>\n<blockquote cite=\"https:\/\/qualitycoding.org\/wwdc21-unit-testing\/\">\n<p>Once or twice in my career, I&rsquo;ve written an abstract test suite as a superclass, where the subclasses provide some sort of factory method. Then all the test cases in that suite get repeated for the specific instances.<\/p>\n<\/blockquote>\n\n<p>I do this all the time. It&rsquo;s useful when I&rsquo;m making a new, optimized version of a class and want to make sure it passes all the same tests as the original. And sometimes I need multiple implementations because certain APIs are broken on certain versions of the operating system.<\/p>\n\n<blockquote cite=\"https:\/\/qualitycoding.org\/wwdc21-unit-testing\/\">\n<p>Maybe you&rsquo;ve wanted to repeat the same tests for a different object. For example, you may want to run the same test cases over different types that implement a protocol. Maybe you tried defining a generic <code>XCTestCase<\/code>, hoping to reuse the test suite across a few types. If you tried this before, you know that it didn&rsquo;t work. <code>XCTest<\/code> uses introspection and follows specific rules to gather test cases and test suites. It didn&rsquo;t pick up anything generic.<\/p>\n<p>But Xcode 12.5 adds support for generic test suites. Here&rsquo;s how it works. Write a generic subclass of <code>XCTestCase<\/code>.<\/p>\n<p>[&#8230;]<\/p>\n<p>And unlike the days when I did this with a quasi-abstract base class, <code>XCTest<\/code> will not run any tests for the top-level generic suite.<\/p>\n<\/blockquote>\n\n<p>This is great. Also, note that in simpler cases his example with a factory method:<\/p>\n\n<pre>\nclass AbstractSuite&lt;T&gt;: XCTestCase { &#8230; }\nclass OneClassTests: AbstractSuite&lt;OneClass&gt; {\n    override func makeSpecificObject() -> OneClass? {\n        \/* Make instance of OneClass*\/\n    }\n}\n<\/pre>\n<p>is more than you need. I often want to test a family of classes that all follow the same interface <code>I<\/code>. Then you can just do:<\/p>\n<pre>\nclass AbstractSuite&lt;T: I&gt;: XCTestCase { &#8230; }\nclass OneClassTests: AbstractSuite&lt;OneClass&gt; {}<\/pre>\n<p>and have the base class call <code>T()<\/code> directly. The subclasses don&rsquo;t need any code.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/02\/02\/xcode-12-5-beta\/\">Xcode 12.5 Beta<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Jon Reid: Once or twice in my career, I&rsquo;ve written an abstract test suite as a superclass, where the subclasses provide some sort of factory method. Then all the test cases in that suite get repeated for the specific instances. I do this all the time. It&rsquo;s useful when I&rsquo;m making a new, optimized version [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-07-08T16:06:37Z","apple_news_api_id":"9a58dd67-5732-439e-a24c-6398c92d3572","apple_news_api_modified_at":"2021-07-08T16:06:38Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AmljdZ1cyQ56iTGOYyS01cg","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[30,1891,71,901,268,226],"class_list":["post-33080","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-mac","tag-macos-11-0","tag-programming","tag-swift-programming-language","tag-testing","tag-xcode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=33080"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33080\/revisions"}],"predecessor-version":[{"id":33081,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33080\/revisions\/33081"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=33080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=33080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=33080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}