Just a FRICKIN HINT
Tick the UnitTests box

Tick the box

Have I got a bone to pick with the Apple Compiler messaging people... A hint to go tick the checkbox for Unit Testing would save me day's of wondering/googling/pondering... and think it's something I've CODED wrong... it's just the frecking Unit Tests check box!

Cannot convert value of type 'UnitTests.Chart_ViewModel' to expected argument type 'AssetsFolio.Chart_ViewModel'

I get that some classes will NOT be part of the Unit Tests Target... but wouldn't the flip-side of this check-box toggle be the better default? Wouldn't it be a better coder experience to have to opt-out of the Unit Test Target Membership?

Having experienced the nightmare of missing this checkbox on so many files in my project - I will most likely learn and remember this experience. But if you have never experienced this nightmare. The error message is not very helpful. And a hint... WOW - that could make someone's day go so much smoother.

Doing it WRONG

So several people on iosdev.Space Mastondon have said I'm (my words) doing it all wrong! That I should NOT have to tick that Target Membership for UnitTesting box.

So today - instead of making RuleMarks on the Stock graph for Buy Transactions - I'm experimenting with Xcode Unit Test configurations ... I'd have to say this task is one of my very least favorite things in the development life cycle.

Here is Jon Reid's gracious instruction on setting up Unit Testing (the hard way - by retrofitting your project settings). Hey - he wrote the book on Unit Testing iOS Code - so he should know.

iOS Unit Testing: The Definitive Guide

"My book iOS Unit Testing by Example: XCTest Tips and Techniques Using Swift is the definitive guide to unit testing iOS apps. It covers foundational tools and skills, testing specific behaviors of iOS apps, and how to use the fast feedback from your tests."
- Jon Reid



My settings

Before I attempt to implement Jon's suggestion and untick the box... I thought I'd see just what my current project settings are...

In the Targets: UnitTests > Build Setting tab :: Linking Section (hint: searched for Bundle Loader) I have the AssetsFolio (app) as the Debug & Release Loader - Jon suggested an Env Var $(Test_Host). I don't know if these are equalivant or different... need to find where Test_Host is defined. Searching... it seams that EnvVar is undefined. Now this leaves me to ponder...


Moving along...

TICKED - Allow Testing Host Application APIs

OK - UnitTests - Build Phases - Target Dependencies - AssetsFolio (my App name)

OK - AssetsFolio (target) - Build Settings - Build Options - Enable Testability : Yes

So all inspected - it appears I've got the configuration Jon has recommended (if we ignore the unspecified bundle loader thingy... what-ever-it-does).

My Test

I unticked the Target Membership box for Chart_ViewModel.swift -> BOOM! compile errors for the Test Case.

I don't know how much comformation bais I should have.... but it is growing.

What's the harm?

Maybe... someone would point out to me the harm I'm inflecting upon myself by ticking that checkbox. What does that really do? Besides making my code compilable for Test Cases?

References:

How to Get Started with iOS Unit Tests in Swift - X Team

There is this extra @testable import declaration. This is Apple’s way of helping you import your app code into the unit test target so you can run your tests. When you use @testable, there is no need to give any files in your application membership into your test target. Apple does this for you, creating a nice and clean separation.

Getting Started with Swift Unit Testing in Xcode - Swift Dev Journal

Getting Started with Unit Tests in Swift - SwiftLee


Let's see....

What happens if I put the TestCase in the target membership of the App AssetsFolio?

Different compile errors... interesting it can not find the XCTest class now. So this is not the way... it was just a lark.

Doing a Reset...

The number of people that are telling me I'm doing it WRONG has me worried... even when what I am doing appears to be working without problems. So what do you do when the evidence and your experience say one thing - yet the experience and advice of others suggest an exclusive other thing? Do you just ignore the massing... or do you question your own practice?

Could both of us be mostly correct... just incorrect in some tiny way? Some way that has not yet been surfaced by the dialogue nor the experiments? My huntch is this could be a possible truth.

So with that reasoning... I'm unchecking the tick box... now because I've got around 50 source files... to untick the Target Membership box... this is a delicate operation... of course I get it Wrong! the first time. But after one failure, I get them all unticked... and my code starts to compile with no errors. A miricle occurs. The tests run and pass - the configuration appears to be functioning as everyone is saying... I do NOT need to tick the Target Membership box. I cannot believe my eyes. So I of course tick the box and recompile the tests... and they fail... untick... compile and pass!

Stop for a bit... consider drinking a bottle of burbon... think for a moment - what could be happening?

I found one article that hinted that the @testable was an Apple improvement. So I'm guessing that the tick box for Target Membership is a backward compatible technique used BEFORE we had the modern @testable convenience to include the system under test in the TestCase.

Summarize Learning...

It would appear that ticking that box is a "bad idea" because it leads to an endless manual process of ticking the box for everything - ONCE ticked for one source file - one must continue the process of putting all source files into the Target membership of the UnitTests. But if you never tick the box... well then using the @testable import AssetsFolio TestCase annotation works to import your source code. I think the two techniques don't play together nicely.

But if one mistakenly ticks that box... well then Pandora's box is slung wide open and all sorts of demons come out to play.

I would bet that if you wanted to get out your crampons and go spelunking in the derivedData folders of the compiler output - you could find the evidence of object files in the path when that tick box is checked.

Purpose of UI tickbox

Jan, (@jandamm) told me the purpose of the UI tick box... for multi-Target code bases. Good to know. I've not played in that arena so no iOS experience there, and experience is true useful knowledge.

He also told me the harm... or the consequences of that ticked box... that Xcode is doing double compiles of your source - once for each Target! Yeah, I did notice a quicker compile cycle using the @target import <App> directive without the tick - source targeted to UnitTest also.