ios app如何“知道”运行单元测试

前端之家收集整理的这篇文章主要介绍了ios app如何“知道”运行单元测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道我可以用 xcodebuild开始我的应用程序的单元测试,但我想知道是什么告诉应用程序在启动期间运行测试,它是一个发送到应用程序的特殊参数,还是以不同的方式编译以运行测试(与XCTest)?

解决方法

Xcode使用xctest和RunTargetUnitTests脚本(来自/ Developer / Tools / RunTargetUnitTests)进行单元测试.

xctest将动态库(测试工具包具有单独的线程作为其作业)注入二进制文件,注入使dylib访问您的进程内存(它可以访问您的进程内存中的类/实例),并且它能够执行调用和make单元测试.从调试器接收来自设备/模拟器的回调(没有用于单元测试的特殊技术).

简单地说:带有测试方案的项目像往常一样编译,但是它会链接动态库,它会使你的进程内存变成木马并进行测试.

还有非常有用的信息:

RunUnitTests接受环境变量,这里有一些有趣的变量

TEST_HOST – 要“注入”的可执行文件的完整路径
指定的单元测试包.对于应用程序,这必须是完整的
在其包装器中的应用程序的路径.不要将其设置为框架
作品或图书馆.

TEST_RIG – 用作测试装备的可执行文件的完整路径
无论是CPlusTestRig还是otest.可执行文件必须采用路径
以测试包作为最终参数.它的DYLD_FRAMEWORK_PATH和
DYLD_LIBRARY_PATH将配置为指向BUILT_PRODUCTS_DIR
在执行之前.如果您使用其中一个默认设置,请不要设置此项
试验台.


BUNDLE_LOADER用作链接器选项,指示将测试动态库链接到指定二进制文件链接器.

测试包目标模板在最后调用/ Developer / Tools / RunUnitTests的shell脚本构建阶段. RunUnitTests查看它通过其环境传递的构建设置,并根据该信息确定如何在测试包中运行测试.

如果您正在测试框架,RunUnitTests将运行相应的测试装备并告诉它加载并运行捆绑中的测试.由于您的测试包应链接到您的框架,因此当测试装置加载您的包时,您的框架将被加载.

如果您正在测试应用程序,则需要在其配置的构建设置中将应用程序指定为测试包的测试主机和分发包加载器. Bundle Loader设置告诉链接器将您的bundle与正在加载它的应用程序链接,就像应用程序是一个框架一样,允许您从bundle中引用应用程序中的类和其他符号,而不实际将它们包含在bundle中.测试主机设置告诉RunUnitTests启动指定的应用程序并将测试包注入其中以运行其测试.

有关更多信息,请参阅RunTargetUnitTests / xctest的手册页

https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/RunTargetUnitTests.1.html#//apple_ref/doc/man/1/RunTargetUnitTests

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,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
原文链接:https://www.f2er.com/iOS/331125.html

猜你在找的iOS相关文章