[Swift] Identify not XCTest against XCUITest and EarlGrey

XCTestでは、Swiftの時にAppDelegateを取得しようとするとクラッシュする問題があります。
そのために、

XCTAssert(NSClassFromString("XCTest") != nil)
XCTAssert(NSClassFromString("XCTestCase") != nil)
XCTAssert(ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil)

などを使い、XCTestである場合はmockしたAppDelegateを使うなどの対策を行うことがあります。ただ、この場合だと、 XCUITestEarlGrey によるテストを行うときもこの判定に引っかかります。

以下の差分には、XCUITestとEarlGreyに置ける上記判定を入れています。両方とも同様に判定が行われます。

https://github.com/KazuCocoa/test.examples/commit/cd3ec48b741320071471b27b3ad6254ce4fbacd5

これを避けるために、例えば環境変数でEarlGreyであることを指定する、などが必要。

XCTAssert(ProcessInfo.processInfo.environment["EarlGrey"] == "true")

https://github.com/KazuCocoa/test.examples/commit/aa527223208d4d56260fa0111ccf158554c0d12f

プロセスに対する環境変数、iOSのテストだとちょくちょくお世話になることがあると思うので、覚えておくと良さそう。xcodebuildなどに対して環境変数を与えると同じようなことになりますが、これはプロセスに対する環境変数なので注意が必要(プロセス外で定義した環境変数は参照されない)

Advertisements

[Android]Run orchestrator 1.0.0

Download apks from maven:

And install them and start the process like https://developer.android.com/training/testing/junit-runner.html

adb install -r path/to/orchestrator-1.0.0.apk
adb install -r path/to/test-services-1.0.0.apk

# Replace "com.example.test" with the name of the package containing your tests.
adb shell 'CLASSPATH=$(pm path android.support.test.services) app_process / \
  android.support.test.services.shellexecutor.ShellMain am instrument -w -e \
  targetInstrumentation com.example.test/android.support.test.runner.AndroidJUnitRunner \
  android.support.test.orchestrator/.AndroidTestOrchestrator'

If you have a custom runner, you can replace android.support.test.runner.AndroidJUnitRunner to yours.

After the above, you can start instrumentation tests via the orchestration layer.

./gradlew connectedAndroidTest

[Kotlin]Kotlin in Actionをようやっと終えた

長らく読んでいたのですが、Kotlin in Actionをようやく読み、写経などし終えた。
他にも同時にやっていたとはいえ、このときからすると時間としては結構長かった…

ただ、Kotlin全体の形や、以下のような処理を書いたりしてというところが理解・発想できるようになったぶん、読んで全体を知ることはよかったかなと思います。

以下は、KotlinでSpekといった大きなライブラリを使うほどではないけれど、テストにコンテキストを持ち込みたい時なんかにプロジェクトに書いておくと良いのではないかな、と思うやつです。

class Then {
}

class When {
    fun then(description: String, then: Then.() -> Unit) {
        then.invoke(Then())
    }
}

class Given {
    fun `when`(description: String, `when`: When.() -> Unit) {
        `when`.invoke(When())
    }
}

fun given(description: String, given: Given.() -> Unit) {
    given.invoke(Given())
}

given("description for given") {
  `when`("description for when") {
    then("description for then") {
      println("finish")
    }
  }
}

KotlinだとWhenが予約語になっているので、Spekででもなのですが、Given/On/Itスタイルにすると良さそう。

class It {
}

class On {
    fun it(description: String, it: It.() -> Unit) {
        it.invoke(It())
    }
}

class Given {
    fun on(description: String, on: On.() -> Unit) {
        on.invoke(On())
    }
}

fun given(description: String, given: Given.() -> Unit) {
    given.invoke(Given())
}

given("description for given") {
  on("description for on") {
    it("description for it") {
      println("finish")
    }
  }
}

すぐに書くことはできるものですが、一応、ここではライセンスをMIT Licenseにしておきます。

The MIT License (MIT)

Copyright (c) 2015 Kazuaki MATSUO

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

[Android]composer and swarmer

I’ve posted [Android][iOS]Awesome tips about composer and swarmer.

The following is my trial with them.

https://github.com/KazuCocoa/run_parallel_tests_android

[Android]Checking Android Testing Support Library 1.0

update: Aug 9, 2017

I encountered no tests found error when running tests with AssertJ and AndroidJUnitRunner1.0.0.
https://stackoverflow.com/questions/45402645/instrumented-tests-failure-with-androidjunitrunner-1-0-0-and-assertj


A few days ago, Android Testing Support Library 1.0 was released.
I pick up some awesome stuff for me, and I think this release will help enhance test automation for other 3rd party libraries.

IdelingResources

help synchronise against

  • Executors
    • com.android.support.test.espresso.idling:idling-concurrent:3.0.0
  • network requests and responses
    • com.android.support.test.espresso.idling:idling-net:3.0.0

New view matchers/actions/methods

Parameterised testing

GrantPermissionRule

Understand how to write/think test for Android

Android Test Orchestrator

Runner related command options

  • -e classLoader – Provide the ability to pass class loaders using runner args
  • -e filter – Add support for custom JUnit filters to be specified using runner args
  • -e runnerBuilder – Allows developers to provide their own implementations of RunnerBuilder that can determine whether and how they can run against a specific class

[Erlang][Software Test]Property-based testing/QuickCheck Testing

I often see Property-based testing recently, and the following article is useful to understand what property-based testing and quick check testing is I’ve read for a few years.

PropEr Testing
http://propertesting.com/

I use QuickCheck testing in my work, and it works fine against functions especially stateless ones.

[iOS]Large Photo Libraries for Testing

from https://developer.apple.com/videos/play/wwdc2017/505/

If you’d like to test with a large number of photos, you can generate dummy photos by https://developer.apple.com/sample-code/wwdc/2017/Creating-Large-Photo-Libraries-for-Testing.zip . Download it and run on the target device.

The app is presented in https://developer.apple.com/videos/play/wwdc2017/505/ .

https://developer.apple.com/documentation/photos/phasset

[Android][iOS]Awesome tips

Headless simulators

Appium(with WDA) can run headless simulators isHeadless against Xcode9+.
Awesome: https://github.com/appium/appium-xcuitest-driver/pull/472/files

composer, swarmer and mainframer

Replace Spoon for Espresso.

[Mobile]Security Testing tools

[Appium]Get invisible elements

uiautomator2 start supporting findInvisibleElement.
https://github.com/appium/appium-uiautomator2-server/issues/61

After the following method, you can get invisible elements from uiautomator2.

update_settings allowInvisibleElements: true

https://github.com/appium/ruby_lib/blob/cc913edb4bc24c18a58c2c5490dfb7b5a705d303/lib/appium_lib/common/command.rb#L46