如何使用 XCTest 在 Xcode 中测试 Swift 文件而无需构建整个应用程序? [英] How can I test Swift files in Xcode using XCTest without having to build the entire app?

查看:29
本文介绍了如何使用 XCTest 在 Xcode 中测试 Swift 文件而无需构建整个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是在对文件进行微小更改后经常测试 Swift 文件.该文件可能包含一个或多个结构和类.

What I'm trying to do is test a Swift file frequently after making tiny changes to the file. The file may contain one or more structs and classes.

我不关心应用"的概念,因为这纯粹是一种学术练习,用于练习我学到的一些新概念.我只是想测试文件,不关心应用程序和与应用程序相关的任何事情.

I don't care about a concept of an "app" because this is purely an academic exercise to practice some new concepts I've learned. I just want to test the file and don't care about the app and anything related to the app.

你可能会问为什么?速度.我希望它的测试速度非常快,现在最简单的例子需要几秒钟.我看到构建目标做了很多工作:

You may be asking why? Speed. I want it to be really fast to test, right now it takes a few seconds for the most trivial example. I see it does a lot of work to build the targets:

推荐答案

你应该试试 Swift Playground.

You should try a Swift Playground.

这是 developer.apple.com/swift 上的说明:

互动游乐场

Playgrounds 让编写 Swift 代码变得异常简单和有趣.输入一行代码,结果立即出现.如果您的代码随时间运行,例如通过循环,您可以在时间线助手中查看其进度.时间线以图形方式显示变量,在组成视图时绘制每个步骤,并且可以播放动画 SpriteKit 场景.当您在 Playground 中完善了您的代码后,只需将该代码移动到您的项目中即可.游乐场的一些用途包括:

Playgrounds make writing Swift code incredibly simple and fun. Type a line of code and the result appears immediately. If your code runs over time, for instance through a loop, you can watch its progress in the timeline assistant. The timeline displays variables in a graph, draws each step when composing a view, and can play an animated SpriteKit scene. When you’ve perfected your code in the playground, simply move that code into your project. Some uses for playgrounds include:

  • 设计一个新算法,每一步都观察它的结果

  • Design a new algorithm, watching its results every step of the way

创建新测试,在升级到您的测试套件之前验证它们是否有效

Create new tests, verifying they work before promoting into your test suite

尝试新的 API 以磨练您的 Swift 编码技能

Experiment with new APIs to hone your Swift coding skills

这篇关于如何使用 XCTest 在 Xcode 中测试 Swift 文件而无需构建整个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆