unittest
学习iOS开发 (Learning iOS Development)
You probably have learned how to add dependencies to your iOS project using Cocoapod, and even create your own local pod for better modularizing your code. But one thing is missing, how do you enable Unit Test to your local pod?
Ÿ欧可能已经学会了如何依赖添加到您的iOS项目中使用Cocoapod ,甚至创建自己的本地荚更好的模块化你的代码。 但是缺少一件事,如何对本地pod启用单元测试?
When creating a local pod, there’s a question below asked, we might think it will create for us. Unfortunately, it is not so.
创建本地pod时,下面会出现一个问题,我们可能会认为它会为我们创建。 不幸的是,事实并非如此。
When you try to create a test in the pod, the Test Run Indicator as shown in red below, will not appear 🧐.
当您尝试在广告连播中创建测试时,如下图红色所示的“测试运行指示器” 将不会 出现 。
How to enable UnitTest for our Local Pod code?
如何为我们的本地Pod代码启用UnitTest?
I google for a day, before able to figure out how to do it. Hence after learning it, let me share it with all.
在能够弄清楚如何做之前,我在Google上搜索了一天。 因此,在学习之后,让我与所有人分享。
It involves 2 steps as below (assuming you already have your local pod as shared in how you can create your own local pod)
它涉及以下2个步骤(假设您已经共享了本地容器,如何创建自己的本地容器 )
1.为您的Pod创建测试规格 (1. Create Your Test Spec for Your Pod)
When your local pod is created, you’ll have the .podspec
file created. It doesn’t set up your test spec for you by default even though you state that you’ll be using quick
as your test framework.
创建本地Pod后,将创建.podspec
文件。 默认情况下,它不会为您设置测试规范,即使您声明您将使用quick
作为测试框架。
In it, you specify the test libraries, and also the location of the test-files.
在其中,您可以指定测试库以及测试文件的位置。
s.test_spec 'UnitTests' do |test_spec|
test_spec.source_files = 'LocalPod/UnitTests/**/*'
test_spec.dependency 'Nimble', '~> 8.0.9'
test_spec.dependency 'Quick', '~> 2.2.0'
end
After you’re done with that, you need to also get your main project Podfile
to state where to find your local pod test spec.
完成之后,您还需要获取主项目Podfile
以说明在哪里可以找到本地Pod测试规范。
pod 'LocalPod',
:path => './modules/LocalPod', :testspecs => ['UnitTests']
Note the UnitTests
above is just a name you provide to your pod spec. It has to match both in your local pod .podspec
and your main project Podfile
.
请注意,上面的UnitTests
只是您提供给pod规范的名称。 它必须与本地pod .podspec
和主项目Podfile
。
After that, remember to run pod install
to sync the dependencies properly.
之后,请记住运行pod install
来正确同步依赖项。
Note: You’ll need to have at least one test file in your test spec source files folder that you specified through
test_spec.source_files
. Else when you dopod install
, you’ll get the below error message.注意:您需要在通过
test_spec.source_files
指定的测试规范源文件文件夹中至少有一个测试文件。 否则,当您执行pod install
,您会收到以下错误消息。
[!] Unable to install the `LocalPod` pod, because the `LocalPod-Unit-UnitTests` target in Xcode would have no sources to compile.
2.将Pod测试纳入项目测试计划 (2. Include Your Pod Test in Project Test Scheme)
After completing step 1 above. You can now open your project workspace.
完成上述步骤1之后。 现在,您可以打开项目工作区。
The Cocaopod files are all fully linked up, but Xcode is not aware of it yet. So you need to tell it.
Cocaopod文件都已完全链接在一起,但是Xcode尚不知道。 因此,您需要告诉它。
To let Xcode know your new unit test location, you need to edit the scheme as shown in diagram below.
为了让Xcode知道您的新单元测试位置,您需要编辑方案,如下图所示。
When you are in the scheme configuration page, pick the Test
scheme, and then click +
to add your new local pod unit test location to it.
在方案配置页面中时,选择“ Test
方案,然后单击+
将新的本地Pod单元测试位置添加到其中。
A dialog will pop up, and you’ll get to see your local pod unit test for you to select. You can now Add
it.
将弹出一个对话框,您将看到要选择的本地Pod单元测试。 您现在可以Add
它。
With that done, when you add your Unit Test swift file to your UnitTest
location as shown below and your test file will have the Test Run Indicator, where you can run the test.
完成此操作后,将单元测试快速文件添加到UnitTest
位置(如下所示),并且测试文件将具有“测试运行指示器”,您可以在其中运行测试。
And if you play the entire test run (as per shown in the hammer sign below), your local pod unit test will be executed together.
而且,如果您进行了整个测试运行(如下面的锤子符号所示),则将一起执行您本地的广告单元测试。
You can get the sample project code here. Also, credit to this StackOverflow that enlighten me on how to do it.
您可以在此处获取示例项目代码 。 另外,值得一提的是这个StackOverflow ,它启发了我如何进行操作。
If you’re having a different configuration setting, do read the following article.
如果您使用其他配置设置,请阅读以下文章。
unittest