0

I have this super basic app which changes the colour of a circle when you press a button:

struct ContentView: View {
    @State private var color: Color = .red
    
    var body: some View {
        VStack {
            Circle()
                .frame(width: 100, height: 100)
                .foregroundColor(color)
            
            Button("Press me") {
                color = color == .red ? .blue : .red
            }
        }
    }
}

All I want to do is write a test which checks if the button appropriately changes the colour of the circle.

I only just read about all the unit testing and UI testing capabilities of Xcode but am unsure how to go about this. From the resources I've read online they all recommend either Snapshot Testing or using a package called ViewInspector. However I just want to play around with the inbuilt capabilities of Xcode and am not looking for any rigorous testing frameworks or methods. Could someone tell me how Apple recommends to test SwiftUI Views? If there's no official/recommended way can someone tell me how to write a unit/UI test to check if the circle's colour changed appropriately.

Thanks in advance!

rayaantaneja
  • 1,182
  • 7
  • 18
  • https://www.appcoda.com/ui-testing-swiftui-xctest/ – lorem ipsum Jul 07 '21 at 18:49
  • @loremipsum I've read that article and I don't think it mentions how to check if the button executed its action correctly. If you could write a piece of code demonstrating how to do it it'd be extremely helpful. – rayaantaneja Jul 07 '21 at 18:55

1 Answers1

1

This thread states that

XCTest is for functional testing, rather than asserting visual requirements

Which is true. So what you can do if you don't want to install the ViewInspector dependency, you can

  1. Make your @State var color: Color have the default access modifier.
  2. Have a function that toggles the color
  3. Create an instance of the View
  4. Test it.

Refer the screenshot below. Note that this has been done on playground, but the same can be referred on an actual project

enter image description here

Visal Rajapakse
  • 1,718
  • 1
  • 9
  • 15
  • Is there a particular way apple recommends doing such a thing? Or is this the best method? Thank you for such a quick answer btw! – rayaantaneja Jul 07 '21 at 19:16
  • FYI, I changed my code a bit. And not really. So XCTests are for functionality testing. Apple doesn't have any recommendations on how to **VALIDATE your UI**, although you can hack your way through it, which I have shown. – Visal Rajapakse Jul 07 '21 at 19:19
  • Also how did you get your return arrow to be a proper arrow instead of "->" – rayaantaneja Jul 07 '21 at 19:20
  • Ah okay. If this is a "hack" to get the job done should I steer away from doing this in real projects? I'm assuming in a real project I should either use snapshot testing or the `ViewInspector` dependency – rayaantaneja Jul 07 '21 at 19:22
  • you have to enable `Font ligatures` on Xcode. Not sure if all fonts do the trick by the one I & many other devs seem to use is `Fira Code` [here](https://github.com/tonsky/FiraCode) – Visal Rajapakse Jul 07 '21 at 19:25
  • Well, If you want to do it, you can. But it might need you to restructure your entire `struct/class` which isn't gonna be as easy in a big/moderately large project as it was to refactor this. So if you feel it, you can. But mostly, it's just the functionality testing that we do – Visal Rajapakse Jul 07 '21 at 19:28