Operating System এবং Custom Conditions ভিত্তিক Test চালানো

Repeated এবং Conditional Tests - জেইউনিট (JUnit) - Java Technologies

263

JUnit একটি জনপ্রিয় টেস্টিং ফ্রেমওয়ার্ক যা Java প্রোগ্রামিং ভাষায় ইউনিট টেস্ট লেখার জন্য ব্যবহৃত হয়। JUnit ব্যবহার করে আপনি সহজেই টেস্ট কন্ডিশন এবং বিভিন্ন অবস্থা (যেমন অপারেটিং সিস্টেম বা কাস্টম কন্ডিশন) অনুযায়ী টেস্ট চালাতে পারেন। JUnit-এ টেস্ট কন্ডিশন বা কাস্টম শর্তাবলী দিয়ে টেস্ট চালানো সাধারণত Assume বা @EnabledIf, @DisabledIf অ্যানোটেশন ব্যবহার করে করা হয়।

এই টিউটোরিয়ালে, আমরা দেখব কিভাবে অপারেটিং সিস্টেম এবং কাস্টম শর্তাবলী ভিত্তিক টেস্ট চালানো যায় JUnit এর মাধ্যমে।


১. JUnit এ Operating System ভিত্তিক Test চালানো

JUnit এর মাধ্যমে আপনি নির্দিষ্ট অপারেটিং সিস্টেমে টেস্ট চালাতে পারেন। এটি সাধারণত System Property বা Assume ক্লাস ব্যবহার করে করা হয়। উদাহরণস্বরূপ, আপনি যদি একটি টেস্ট ফাংশন চালাতে চান যা শুধুমাত্র Windows অপারেটিং সিস্টেমে চলে, তবে আপনি Assume ব্যবহার করতে পারেন।

উদাহরণ: Windows OS ভিত্তিক টেস্ট

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assumptions;

public class OsBasedTest {

    @Test
    void testOnWindows() {
        String os = System.getProperty("os.name").toLowerCase();
        Assumptions.assumeTrue(os.contains("win"));
        
        // Test code that should run only on Windows OS
        System.out.println("This test runs only on Windows.");
    }

    @Test
    void testOnMac() {
        String os = System.getProperty("os.name").toLowerCase();
        Assumptions.assumeTrue(os.contains("mac"));

        // Test code that should run only on macOS
        System.out.println("This test runs only on macOS.");
    }
}

এখানে, Assume.assumeTrue() ব্যবহার করে নিশ্চিত করা হয়েছে যে টেস্টটি শুধুমাত্র Windows বা macOS অপারেটিং সিস্টেমে চালানো হবে। যদি শর্তটি সঠিক না হয়, তবে টেস্টটি বাদ দেওয়া হবে (Skipped)।


২. JUnit এ Custom Conditions ভিত্তিক Test চালানো

JUnit 5-এ @EnabledIf এবং @DisabledIf অ্যানোটেশন ব্যবহার করে আপনি কাস্টম শর্তাবলী ভিত্তিক টেস্ট চালাতে পারেন। উদাহরণস্বরূপ, আপনি কোনো নির্দিষ্ট শর্ত পূর্ণ হলে টেস্ট চালাতে পারেন এবং অন্যথায় টেস্টটি বন্ধ রাখতে পারেন।

উদাহরণ: Custom Condition ভিত্তিক টেস্ট চালানো

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.condition.DisabledIf;

public class CustomConditionTest {

    @Test
    @EnabledIf("customCondition")
    void testEnabledIfCondition() {
        System.out.println("This test is enabled because the custom condition is true.");
    }

    @Test
    @DisabledIf("customCondition")
    void testDisabledIfCondition() {
        System.out.println("This test is disabled because the custom condition is true.");
    }

    boolean customCondition() {
        // Your custom condition logic (e.g., checking some property or system state)
        return true; // Example: returns true to enable/disable test
    }
}

এখানে, @EnabledIf এবং @DisabledIf অ্যানোটেশন ব্যবহার করা হয়েছে, যেখানে customCondition() মেথডটি কাস্টম শর্ত পরীক্ষা করে। যদি customCondition() সত্য (true) হয়, তাহলে টেস্টটি চালানো হবে অথবা বন্ধ করা হবে।


৩. JUnit 5 Condition Annotations: @EnabledIf, @DisabledIf

JUnit 5-এর @EnabledIf এবং @DisabledIf অ্যানোটেশনগুলো আপনাকে বিভিন্ন কন্ডিশন ভিত্তিক টেস্ট চালানোর সুযোগ দেয়। আপনি এই অ্যানোটেশনগুলো ব্যবহার করে শর্ত নির্ধারণ করতে পারেন।

@EnabledIf অ্যানোটেশন

@EnabledIf ব্যবহার করলে একটি টেস্ট শুধুমাত্র তখনই চালানো হবে যখন নির্দিষ্ট শর্ত পূর্ণ হবে। এই শর্তটি একটি কাস্টম শর্ত হতে পারে, যেমন একটি সিস্টেম প্রোপার্টি বা কোনো ফাইলের উপস্থিতি।

@EnabledIf("java.lang.System.getProperty('os.name').contains('Linux')")
void testOnLinux() {
    // This test runs only on Linux OS
}

এখানে, টেস্টটি শুধুমাত্র তখন চালানো হবে যদি অপারেটিং সিস্টেমের নাম "Linux" থাকে।

@DisabledIf অ্যানোটেশন

@DisabledIf ব্যবহার করলে একটি টেস্ট তখনই বাদ দেওয়া হবে (skipped) যখন নির্দিষ্ট শর্ত পূর্ণ হবে।

@DisabledIf("java.lang.System.getProperty('os.name').contains('Windows')")
void testOnNonWindows() {
    // This test will be disabled if the OS is Windows
}

এখানে, টেস্টটি Windows OS-এ চলে না, অর্থাৎ, Windows এ থাকলে এটি ডিসেবল হয়ে যাবে।


৪. JUnit 5 Conditional Execution: Assume Class

JUnit 5-এ Assume ক্লাস ব্যবহার করে শর্ত ভিত্তিক টেস্ট চালানো যায়। Assume ক্লাস ব্যবহার করলে টেস্টটি রান হবে যদি শর্তটি পূর্ণ হয়, অন্যথায় টেস্টটি বাতিল হয়ে যাবে।

উদাহরণ: Conditional Test Execution with Assume

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assumptions;

public class ConditionalTest {

    @Test
    void testOnLinux() {
        String os = System.getProperty("os.name").toLowerCase();
        Assumptions.assumeTrue(os.contains("linux"));
        
        // Test logic that should only run on Linux
        System.out.println("This test runs on Linux only.");
    }

    @Test
    void testOnWindows() {
        String os = System.getProperty("os.name").toLowerCase();
        Assumptions.assumeTrue(os.contains("win"));
        
        // Test logic that should only run on Windows
        System.out.println("This test runs on Windows only.");
    }
}

এখানে, Assume.assumeTrue() ব্যবহার করা হয়েছে, যা Linux বা Windows অপারেটিং সিস্টেমে টেস্ট চালাতে সাহায্য করবে। যদি শর্ত পূর্ণ না হয়, তবে টেস্টটি স্কিপ হয়ে যাবে।


৫. JUnit 5 Execution Conditions

JUnit 5-এ ExecutionConditions ব্যবহারের মাধ্যমে আপনার টেস্টের শর্ত অনুযায়ী টেস্ট কনফিগার করা যায়। উদাহরণস্বরূপ, আপনি কোনো নির্দিষ্ট সিস্টেম প্রপার্টি বা কনফিগারেশন সেটিংস যাচাই করে টেস্ট চালাতে পারেন।

উদাহরণ: ExecutionCondition Custom Logic

@ExecutionCondition
public class CustomExecutionCondition implements Condition {

    @Override
    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            return ConditionEvaluationResult.enabled("Test is enabled on Windows");
        } else {
            return ConditionEvaluationResult.disabled("Test is disabled on non-Windows systems");
        }
    }
}

এখানে, CustomExecutionCondition একটি কাস্টম কন্ডিশন নির্ধারণ করেছে, যা Windows OS তে টেস্ট চালাবে এবং অন্যথায় নিষ্ক্রিয় করবে।


সারাংশ

JUnit 5 আপনাকে Operating System এবং Custom Conditions ভিত্তিক টেস্ট চালানোর জন্য শক্তিশালী কনডিশনাল টেস্টিং ফিচার প্রদান করে। আপনি Assume, @EnabledIf, @DisabledIf, এবং ExecutionCondition ব্যবহার করে নির্দিষ্ট শর্ত পূর্ণ হলে টেস্ট চালাতে পারেন এবং অন্যথায় টেস্ট স্কিপ বা ডিসেবল করতে পারেন। এর মাধ্যমে, আপনি আপনার টেস্ট প্রক্রিয়াকে আরও ফ্লেক্সিবল এবং কাস্টমাইজড করতে পারবেন, যা আপনাকে বিভিন্ন পরিবেশে সঠিকভাবে টেস্ট পরিচালনা করতে সাহায্য করবে।


Content added By
Promotion

Are you sure to start over?

Loading...