我知道我可以使用xcodebuild
开始我的应用程序的单元测试,但我想知道是什么告诉应用程序在启动期间运行测试,它是一个发送给应用程序的特殊参数,还是以不同方式编译的为了运行测试(使用XCTest)?
答案 0 :(得分:15)
Xcode使用xctest
和RunTargetUnitTests
脚本(来自/Developer/Tools/RunTargetUnitTests
)进行单元测试。
xctest
将dynamic library
(具有独立线程的测试工具包)注入到二进制文件中,注入为您的进程内存提供dylib
访问权限(它可以访问您的类/实例)在你处理内存),它能够执行调用和进行单元测试。从调试器接收来自设备/模拟器的回调(没有用于单元测试的特殊技术)。
简单地说:test scheme
项目像往常一样编译,但是它会链接动态库,它会使您的进程内存变为木马,并进行测试。
此信息非常有用:
RunUnitTests
接受ENVIRONMENT variables
,这里有一些有趣的内容
TEST_HOST
- “注入”可执行文件的完整路径
指定的单元测试包。对于应用程序,这必须是完整的
在其包装器中的应用程序的路径。不要将其设置为框架
作品或图书馆。
TEST_RIG
- 用作测试装备的可执行文件的完整路径
无论是CPlusTestRig还是otest。可执行文件必须采用路径
以测试包作为最终参数。它的DYLD_FRAMEWORK_PATH和
DYLD_LIBRARY_PATH将配置为指向BUILT_PRODUCTS_DIR
在执行之前。如果您使用其中一个默认设置,请不要设置此项
试验台。
也
BUNDLE_LOADER
用作链接器选项,指示将Testing dynamic library
链接到指定二进制文件的链接器。
测试包目标模板在最后调用/Developer/Tools/RunUnitTests
的shell脚本构建阶段。 RunUnitTests查看它通过其环境传递的构建设置,并根据该信息确定如何在测试包中运行测试。
如果您正在测试框架,RunUnitTests将运行相应的测试装备并告诉它加载并运行捆绑包中的测试。由于您的测试包应链接到您的框架,因此当测试装置加载您的包时,您的框架将被加载。
如果您正在测试应用程序,则需要在其配置的构建设置中将应用程序指定为测试包的测试主机和分发包加载器。 Bundle Loader设置告诉链接器将您的bundle与正在加载它的应用程序链接,就像应用程序是一个框架一样,允许您从bundle中引用应用程序中的类和其他符号,而不实际将它们包含在bundle中。测试主机设置告诉RunUnitTests启动指定的应用程序并将测试包注入其中以运行其测试。
有关详细信息,请参阅man
/ RunTargetUnitTests
xctest
页
https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/xctest.1.html
这是RunTargetUnitTests
脚本
#!/bin/sh # # Copyright (c) 2005-2013 Apple Inc. All rights reserved. # # Copyright (c) 1997-2005, Sen:te (Sente SA). All rights reserved. # # Use of this source code is governed by the following license: # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # # (1) Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # (2) Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL Sente SA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # Note: this license is equivalent to the FreeBSD license. # # This notice may not be removed from this file. if [ "${NATIVE_ARCH_ACTUAL}" = "" ]; then NATIVE_ARCH_ACTUAL=`arch` fi if [ "${ARCHS}" = "" ]; then ARCHS=`arch` fi if [ "${DEVELOPER_DIR}" = "" ]; then DEVELOPER_DIR="${SYSTEM_DEVELOPER_DIR}" fi if [ "${OTEST}" = "" ]; then OTEST="${DEVELOPER_DIR}/Tools/otest" fi if [ "${OTEST_TARGET}" = "" ]; then OTEST_TARGET="${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}" fi RunTargetUnitTestsForArch() { echo "${DEVELOPER_DIR}/Tools/RunTargetUnitTests:0: note: Starting tests for ${1}" if [ "${DYLD_FRAMEWORK_PATH}" = "" ] ; then DYLD_FRAMEWORK_PATH="${BUILT_PRODUCTS_DIR}:${DEVELOPER_DIR}/Library/Frameworks" else DYLD_FRAMEWORK_PATH="${BUILT_PRODUCTS_DIR}:${DEVELOPER_DIR}/Library/Frameworks:${DYLD_FRAMEWORK_PATH}" fi export DYLD_FRAMEWORK_PATH echo "OTEST=${OTEST}" arch -arch "${1}" "${OTEST}" "${OTEST_TARGET}" unset DYLD_FRAMEWORK_PATH echo "${DEVELOPER_DIR}/Tools/RunTargetUnitTests:0: note: Completed tests for ${1}" } SkipTargetUnitTestsForArch() { echo "${DEVELOPER_DIR}/Tools/RunTargetUnitTests:0: note: Skipped tests for ${1}" } if [ "${TEST_AFTER_BUILD}" = "YES" ]; then # Run the unit tests once per requested and supported architecture. for TEST_ARCH in ${ARCHS}; do case "${NATIVE_ARCH_ACTUAL}" in i386) if [ "${TEST_ARCH}" = "i386" ]; then RunTargetUnitTestsForArch "${TEST_ARCH}" else SkipTargetUnitTestsForArch "${TEST_ARCH}" fi ;; x86_64) if [ "${TEST_ARCH}" = "i386" -o "${TEST_ARCH}" = "x86_64" ]; then RunTargetUnitTestsForArch "${TEST_ARCH}" else SkipTargetUnitTestsForArch "${TEST_ARCH}" fi ;; *) RunTargetUnitTestsForArch "${TEST_ARCH}" ;; esac done fi
答案 1 :(得分:0)
我知道它是如何与OCTest合作的,我认为XCTest以类似的方式工作。
我曾经使用WaxSim
运行我的测试,这是一个访问私有模拟器框架的实用程序。 WaxSim
的使用在这里并不重要,它只是让我们能够检查内部。
运行命令在模拟器中运行MyApp.app
。要开始测试,首先必须注入它们(它们被编译为单独的包!)。该命令看起来像这样
WaxSim \
-e DYLD_INSERT_LIBRARIES="$INJECTION_FRAMEWORK_PATH" \
-e XCInjectBundle="MyApp.octest" \
-e XCInjectBundleInto="MyApp.app/MyApp" \
MyApp.app \
-SenTest All
INJECTION_FRAMEWORK_PATH
的定义如下:
XCODE_PATH=$( xcode-select --print-path )
PLATFORM_PATH="$XCODE_PATH/Platforms/iPhoneSimulator.platform"
INJECTION_FRAMEWORK_PATH="$PLATFORM_PATH/Developer/Library/PrivateFrameworks/IDEBundleInjection.framework/IDEBundleInjection"
另请注意-SenTest All
命令行参数,它们告诉框架要运行哪些测试。
基本上,应用程序一如既往地运行,但是注入了另一个bundle来逐个运行测试,然后退出整个应用程序。