অ্যান্ড্রয়েড স্টুডিওতে টেস্ট এবং কমান্ড লাইন থেকে টেস্ট অংশে বেসিক টেস্ট কনফিগারেশন কীভাবে সেট আপ এবং রান করতে হয় তা ব্যাখ্যা করা হয়েছে। তবে, যখন আপনার অ্যাপ এবং এর টেস্টের প্রয়োজনীয়তা আরও উন্নত হয়, তখন আপনার টেস্ট কনফিগারেশন আরও পরিবর্তন করার প্রয়োজন হতে পারে। উদাহরণস্বরূপ, যখন আপনি নিম্নলিখিত কাজগুলো করতে চান, তখন আপনার অ্যাডভান্সড টেস্ট সেটআপের প্রয়োজন হতে পারে:
- Run instrumented tests only for a specific build variant or override its manifest settings.
- Change the build type your tests run against or configure its Gradle options.
- Extract your instrumented tests into their own test module.
- Perform more advanced testing as part of your Continuous Integration setup.
This page describes various ways to configure your tests when the default settings don't fit your needs.
একটি বিল্ড ভ্যারিয়েন্টের জন্য একটি ইনস্ট্রুমেন্টেড টেস্ট তৈরি করুন
আপনার প্রজেক্টে যদি স্বতন্ত্র সোর্স সেটসহ বিল্ড ভ্যারিয়েন্ট থাকে, তাহলে আপনি সেই সোর্স সেটগুলোর সাথে সঙ্গতিপূর্ণ ইনস্ট্রুমেন্টেড টেস্ট অন্তর্ভুক্ত করতে পারেন। এটি আপনার টেস্ট কোডকে সুসংগঠিত রাখে এবং আপনাকে শুধুমাত্র একটি নির্দিষ্ট বিল্ড ভ্যারিয়েন্টের জন্য প্রযোজ্য টেস্টগুলো চালানোর সুযোগ দেয়।
To link instrumented tests to a build variant, place them in their own source set, located at src/androidTest VariantName .
src/androidTest/ সোর্স সেটের ইন্সট্রুমেন্টেড টেস্টগুলো সমস্ত বিল্ড ভ্যারিয়েন্টের জন্য ব্যবহৃত হয়। আপনার অ্যাপের "MyFlavor" ভ্যারিয়েন্টের জন্য একটি টেস্ট APK বিল্ড করার সময়, Gradle src/androidTest/ এবং src/androidTestMyFlavor/ সোর্স সেট দুটিকে একত্রিত করে।
To add a testing source set for your build variant in Android Studio, follow these steps:
- In the Project window, click the menu and select the Project view.
- Within the appropriate module folder, right-click the src folder and click New > Directory .
- For the directory name, enter "androidTest VariantName ." For example, if you have a build variant called "MyFlavor," use the directory name
androidTestMyFlavor. - OK ক্লিক করুন।
- Right-click the new directory and select New > Directory .
- Enter "java" as the directory name, then click OK .
Now you can add tests to this new source set by following the steps to add a new test . When you reach the Choose Destination Directory dialog, select the new variant test source set.
The following table shows an example of how instrumentation test files could reside in source sets that correspond to the app's code source sets:
Table 1. App source code and corresponding instrumentation test files
| অ্যাপ ক্লাসের পথ | ইনস্ট্রুমেন্টেশন পরীক্ষার ক্লাসের সাথে মেলানোর পথ |
|---|---|
src/main/kotlin+java/Example.kt | src/androidTest/java/AndroidExampleTest.kt |
src/myFlavor/kotlin+java/Example.kt | src/androidTestMyFlavor/java/AndroidExampleTest.kt |
আপনার অ্যাপ সোর্স সেটের মতোই, গ্রেডল বিল্ড বিভিন্ন টেস্ট সোর্স সেট থেকে ফাইলগুলোকে মার্জ ও ওভাররাইড করে। এক্ষেত্রে, androidTestMyFlavor সোর্স সেটের AndroidExampleTest.kt ফাইলটি androidTest সোর্স সেটের ভার্সনটিকে ওভাররাইড করে। এর কারণ হলো, প্রোডাক্ট ফ্লেভার সোর্স সেটের অগ্রাধিকার মূল সোর্স সেটের চেয়ে বেশি।
When you select different flavors in the build variants selector, the appropriate androidTest folders are displayed in the Android view to show the folders that are used:

MyFlavor variant selected; the androidTestMyFlavor folder displays in the Android view. The androidTestMyFlavor folder is not shown when a different variant is selected:

OtherFlavor variant selected; the androidTestMyFlavor folder does not show in the Android view.This looks slightly different if you are using the Project view, but the same principle applies:

MyFlavor variant selected; the androidTestMyFlavor folder is active in the Project view. When a different variant is selected, the androidTestMyFlavor folder is still visible, but it is not shown as active:

OtherFlavor variant selected; the androidTestMyFlavor folder is not active in the Project view.For more information about how source sets are merged, see Source sets .
ইন্সট্রুমেন্টেশন ম্যানিফেস্ট সেটিংস কনফিগার করুন
Instrumented tests are built into a separate APK with its own AndroidManifest.xml file. When Gradle builds your test APK, it automatically generates the AndroidManifest.xml file and configures it with the <instrumentation> node. One of the reasons Gradle configures this node for you is to make sure that the targetPackage property specifies the correct package name of the app under test.
এই নোডের অন্যান্য সেটিংস পরিবর্তন করতে, হয় টেস্ট সোর্স সেটে আরেকটি ম্যানিফেস্ট ফাইল তৈরি করুন অথবা আপনার মডিউল-স্তরের build.gradle ফাইলটি কনফিগার করুন, যেমনটি নিম্নলিখিত কোড নমুনায় দেখানো হয়েছে। বিকল্পগুলির সম্পূর্ণ তালিকা BaseFlavor API রেফারেন্সে পাওয়া যাবে।
কোটলিন
android { ... defaultConfig { ... testApplicationId = "com.example.test" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testHandleProfiling = true testFunctionalTest = true } }
গ্রুভি
android { ... defaultConfig { ... testApplicationId "com.example.test" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testHandleProfiling true testFunctionalTest true } }
Each product flavor you configure can override properties in the defaultConfig {} block. To learn more, go to Configure product flavors .
স্নিপেটের প্রোপার্টিগুলো হলো:
| সেটিং | বর্ণনা |
|---|---|
testApplicationId | টেস্ট APK-এর জন্য অ্যাপ্লিকেশন আইডি নির্দিষ্ট করে। |
testInstrumentationRunner | Specifies the fully qualified class name of the test instrumentation runner. |
testHandleProfiling | If set to true , enables the instrumentation class to start and stop profiling.If set to false , profiling occurs the entire time the instrumentation class is running. |
testFunctionalTest | If set to true , indicates that the Android system should run the instrumentation class as a functional test.ডিফল্ট মান হলো false । |
টেস্ট বিল্ড টাইপ পরিবর্তন করুন
ডিফল্টরূপে, সমস্ত ইন্সট্রুমেন্টেশন টেস্ট debug বিল্ড টাইপের বিপরীতে চলে। আপনি আপনার মডিউল-স্তরের build.gradle ফাইলে testBuildType প্রপার্টি ব্যবহার করে এটিকে অন্য বিল্ড টাইপে পরিবর্তন করতে পারেন। উদাহরণস্বরূপ, আপনি যদি আপনার টেস্টগুলো staging বিল্ড টাইপের বিপরীতে চালাতে চান, তাহলে নিম্নলিখিত কোড স্নিপেটে দেখানো অনুযায়ী ফাইলটি সম্পাদনা করুন:
কোটলিন
android { ... testBuildType = "staging" }
গ্রুভি
android { ... testBuildType "staging" }
গ্রেডল পরীক্ষার বিকল্পগুলি কনফিগার করুন
অ্যান্ড্রয়েড গ্র্যাডল প্লাগইন আপনাকে আপনার সমস্ত বা শুধুমাত্র কিছু টেস্টের জন্য নির্দিষ্ট অপশন নির্ধারণ করার সুযোগ দেয়। মডিউল-স্তরের build.gradle ফাইলে, testOptions ব্লকটি ব্যবহার করে সেই অপশনগুলো নির্দিষ্ট করুন যা গ্র্যাডল কীভাবে আপনার সমস্ত টেস্ট চালাবে তা পরিবর্তন করে:
কোটলিন
android { ... // Encapsulates options for running tests. testOptions { reportDir = "$rootDir/test-reports" resultsDir = "$rootDir/test-results" } }
গ্রুভি
android { ... // Encapsulates options for running tests. testOptions { reportDir "$rootDir/test-reports" resultsDir "$rootDir/test-results" } }
The reportDir property changes the directory where Gradle saves test reports. By default, Gradle saves test reports in the path_to_your_project / module_name /build/outputs/reports/ directory. $rootDir sets the path relative to the root directory of the current project.
` resultsDir প্রপার্টিটি সেই ডিরেক্টরি পরিবর্তন করে যেখানে গ্রেডল পরীক্ষার ফলাফল সংরক্ষণ করে। ডিফল্টরূপে, গ্রেডল পরীক্ষার ফলাফল path_to_your_project / module_name /build/outputs/test-results/ ডিরেক্টরিতে সংরক্ষণ করে। $rootDir বর্তমান প্রজেক্টের রুট ডিরেক্টরির সাপেক্ষে পাথ নির্ধারণ করে।
To specify options for only local unit tests, configure the unitTests block inside testOptions .
কোটলিন
android { ... testOptions { ... // Encapsulates options for local unit tests. unitTests { returnDefaultValues = true all { jvmArgs = listOf("-XX:MaxPermSize=256m") if (it.name == "testDebugUnitTest") { systemProperty = mapOf("debug" to "true") } ... } } } }
গ্রুভি
android { ... testOptions { ... // Encapsulates options for local unit tests. unitTests { returnDefaultValues true all { jvmArgs '-XX:MaxPermSize=256m' if (it.name == 'testDebugUnitTest') { systemProperty 'debug', 'true' } ... } } } }
ডিফল্টরূপে, লোকাল ইউনিট টেস্টগুলো একটি এক্সেপশন থ্রো করে যখনই আপনার পরীক্ষাধীন কোডটি অ্যান্ড্রয়েড প্ল্যাটফর্ম এপিআই অ্যাক্সেস করার চেষ্টা করে, যদি না আপনি নিজে অথবা মকিটোর (Mockito) মতো কোনো টেস্টিং ফ্রেমওয়ার্ক ব্যবহার করে অ্যান্ড্রয়েড ডিপেন্ডেন্সিগুলোকে মক করেন । তবে, আপনি ` returnDefaultValues প্রপার্টিটি এনাবল করতে পারেন, যাতে টেস্টটি প্ল্যাটফর্ম এপিআই অ্যাক্সেস করার সময় এক্সেপশন থ্রো করার পরিবর্তে `null` অথবা `zero` রিটার্ন করে।
The all block encapsulates options for controlling how Gradle executes local unit tests. For a list of all the options you can specify, read Gradle's reference documentation .
The jvmArgs property sets JVM argument(s) for the test JVM(s).
You can also check the task name to apply options to only the tests you specify. In the example snippet, the debug property is set to true but only for the testDebugUnitTest task.
ইনস্ট্রুমেন্টেড টেস্টের জন্য আলাদা টেস্ট মডিউল ব্যবহার করুন
আপনি যদি ইন্সট্রুমেন্টেড টেস্টের জন্য একটি ডেডিকেটেড মডিউল রাখতে চান এবং আপনার কোডের বাকি অংশকে টেস্টগুলো থেকে আলাদা করতে চান, তাহলে একটি পৃথক টেস্ট মডিউল তৈরি করুন এবং লাইব্রেরি মডিউলের মতোই এর বিল্ড কনফিগার করুন।
একটি টেস্ট মডিউল তৈরি করতে, নিম্নলিখিত পদ্ধতি অনুসরণ করুন:
- একটি লাইব্রেরি মডিউল তৈরি করুন ।
- In the module-level
build.gradlefile, apply thecom.android.testplugin instead ofcom.android.library. - প্রজেক্ট সিঙ্ক করতে ক্লিক করুন
.
After you create your test module, you can include your test code in the main or variant source set (for example, src/main/kotlin+java or src/ variant /kotlin+java ). If your app module defines multiple product flavors, you can re-create those flavors in your test module. Using variant-aware dependency management , the test module attempts to test the matching flavor in the target module.
By default, test modules contain and test only a debug variant. However, you can create new build types to match the tested app project. To make the test module test a different build type and not the debug one, use VariantFilter to disable the debug variant in the test project, as shown:
কোটলিন
android { variantFilter { if (buildType.name == "debug") { ignore = true } } }
গ্রুভি
android { variantFilter { variant -> if (variant.buildType.name.equals('debug')) { variant.setIgnore(true); } } }
যদি আপনি চান যে একটি টেস্ট মডিউল কোনো অ্যাপের শুধুমাত্র নির্দিষ্ট ফ্লেভার বা বিল্ড টাইপগুলোকে টার্গেট করুক, তাহলে আপনি matchingFallbacks প্রপার্টি ব্যবহার করে কেবল সেই ভ্যারিয়েন্টগুলোকেই টার্গেট করতে পারেন যেগুলো আপনি পরীক্ষা করতে চান। এর ফলে টেস্ট মডিউলটিকে নিজের জন্য সেই ভ্যারিয়েন্টগুলো কনফিগার করার প্রয়োজনও হয় না।