test সাজানো
অধ্যায়ের শুরুতে যেমন বলা হয়েছে, testing একটি জটিল বিষয়, আর ভিন্ন ভিন্ন মানুষ ভিন্ন পরিভাষা ও সাজানোর ধরন ব্যবহার করে। Rust community test-কে দুটি প্রধান শ্রেণিতে ভাবে: unit test আর integration test। Unit tests (unit test) ছোট ও বেশি কেন্দ্রীভূত, একবারে একটি module-কে আলাদা করে test করে, এবং private interface-ও test করতে পারে। Integration tests (integration test) পুরোপুরি আপনার library-র বাইরের, এবং অন্য যেকোনো বাইরের কোড যেভাবে আপনার কোড ব্যবহার করত সেভাবেই ব্যবহার করে, শুধু public interface দিয়ে, আর প্রতিটি test-এ সম্ভবত একাধিক module পরীক্ষা করে।
আপনার library-র অংশগুলো আলাদাভাবে ও একসঙ্গে আপনার প্রত্যাশামতো কাজ করছে কি না তা নিশ্চিত করতে দুই ধরনের test লেখাই গুরুত্বপূর্ণ।
unit test
unit test-এর উদ্দেশ্য হলো কোডের প্রতিটি একককে বাকি কোড থেকে আলাদা করে test করা, যাতে দ্রুত চিহ্নিত করা যায় কোথায় কোড
প্রত্যাশামতো কাজ করছে আর কোথায় করছে না। unit test রাখবেন src directory-তে, যে কোড test করছে সেই কোডের সঙ্গে একই ফাইলে।
প্রচলিত রীতি হলো প্রতিটি ফাইলে test function-গুলো রাখার জন্য tests নামের একটি module তৈরি করা এবং module-টিকে cfg(test)
দিয়ে annotate করা।
tests module ও #[cfg(test)]
tests module-এর ওপর #[cfg(test)] annotation Rust-কে বলে test কোড শুধু cargo test চালালেই compile করে চালাতে, cargo build চালালে নয়। এতে শুধু library build করতে চাইলে compile-এর সময় বাঁচে, আর তৈরি হওয়া compile করা artifact (ফলাফল ফাইল)-এ
জায়গাও বাঁচে, কারণ test-গুলো তাতে থাকে না। আপনি দেখবেন, integration test যেহেতু আলাদা directory-তে থাকে, তাই সেগুলোর
#[cfg(test)] annotation লাগে না। কিন্তু unit test কোডের সঙ্গে একই ফাইলে থাকে বলে #[cfg(test)] দিয়ে বলে দিতে হয় যে
এগুলো compile করা ফলাফলে যুক্ত হবে না।
মনে করুন, এই অধ্যায়ের প্রথম অংশে নতুন adder project তৈরি করার সময় Cargo আমাদের জন্য এই কোড তৈরি করেছিল:
Filename: src/lib.rs
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
স্বয়ংক্রিয়ভাবে তৈরি tests module-এর ওপর থাকা cfg attribute-এর পূর্ণরূপ configuration (কনফিগারেশন), আর এটি Rust-কে
বলে যে পরের item-টি শুধু নির্দিষ্ট একটি configuration বিকল্প থাকলেই যুক্ত হবে। এই ক্ষেত্রে configuration বিকল্পটি হলো test,
যা Rust test compile ও চালানোর জন্য দেয়। cfg attribute ব্যবহার করায় Cargo আমাদের test কোড compile করে কেবল তখনই, যখন আমরা
সক্রিয়ভাবে cargo test দিয়ে test চালাই। এর মধ্যে #[test] দিয়ে annotate করা function ছাড়াও এই module-এ থাকা যেকোনো
সহায়ক function পড়ে।
private function-এর test
private function সরাসরি test করা উচিত কি না তা নিয়ে testing community-তে বিতর্ক আছে, আর অন্য কিছু ভাষায় private function
test করা কঠিন বা অসম্ভব। আপনি testing-এর যে মতাদর্শই মানুন না কেন, Rust-এর privacy-র নিয়ম আপনাকে private function test
করতে দেয়। Listing 11-12-এর কোডটি দেখুন, যেখানে internal_adder নামের একটি private function আছে।
pub fn add_two(a: u64) -> u64 {
internal_adder(a, 2)
}
fn internal_adder(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn internal() {
let result = internal_adder(2, 2);
assert_eq!(result, 4);
}
}
খেয়াল করুন, internal_adder function-কে pub হিসেবে চিহ্নিত করা হয়নি। test শুধুই Rust কোড, আর tests module আরেকটি
module মাত্র। “module tree-র কোনো item উল্লেখ করার path” অংশে যেমন আলোচনা করেছি, child module-এর
item-গুলো তাদের পূর্বপুরুষ module-এর item ব্যবহার করতে পারে। এই test-এ আমরা use super::* দিয়ে tests module-এর parent-এর
সব item scope-এ আনি, তারপর test-টি internal_adder call করতে পারে। আপনি যদি মনে করেন private function test করা উচিত নয়, Rust-এ
এমন কিছু নেই যা আপনাকে তা করতে বাধ্য করবে।
integration test
Rust-এ integration test পুরোপুরি আপনার library-র বাইরের। এগুলো অন্য যেকোনো কোডের মতোই আপনার library ব্যবহার করে, অর্থাৎ শুধু সেই function-গুলোই call করতে পারে যা আপনার library-র public API-র অংশ। এদের উদ্দেশ্য হলো আপনার library-র অনেক অংশ একসঙ্গে ঠিকমতো কাজ করে কি না তা test করা। কোডের যে এককগুলো আলাদাভাবে ঠিকঠাক কাজ করে, একসঙ্গে জোড়া লাগালে সেগুলোতে সমস্যা দেখা দিতে পারে, তাই জোড়া লাগানো কোডের test coverage-ও গুরুত্বপূর্ণ। integration test তৈরি করতে প্রথমে একটি tests directory লাগবে।
tests directory
আমরা project directory-র শীর্ষ স্তরে, src-এর পাশে একটি tests directory তৈরি করি। Cargo জানে যে এই directory-তে integration test ফাইল খুঁজতে হবে। তারপর যত খুশি test ফাইল বানাতে পারি, আর Cargo প্রতিটি ফাইলকে একটি আলাদা crate হিসেবে compile করবে।
চলুন একটি integration test তৈরি করি। Listing 11-12-এর কোড src/lib.rs ফাইলে রেখেই একটি tests directory বানান, আর tests/integration_test.rs নামে একটি নতুন ফাইল তৈরি করুন। আপনার directory-র গঠন দেখতে এমন হওয়ার কথা:
adder
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
└── tests
└── integration_test.rs
Listing 11-13-এর কোডটি tests/integration_test.rs ফাইলে লিখুন।
use adder::add_two;
#[test]
fn it_adds_two() {
let result = add_two(2);
assert_eq!(result, 4);
}
adder crate-এর একটি function-এর integration testtests directory-র প্রতিটি ফাইল একটি আলাদা crate, তাই প্রতিটি test crate-এর scope-এ আমাদের library আনতে হবে। এ কারণে কোডের
শুরুতে আমরা use adder::add_two; যোগ করি, যা unit test-এ দরকার হয়নি।
tests/integration_test.rs-এর কোনো কোডকে #[cfg(test)] দিয়ে annotate করার দরকার নেই। Cargo tests directory-কে বিশেষভাবে
গণ্য করে এবং এই directory-র ফাইলগুলো compile করে কেবল তখনই, যখন আমরা cargo test চালাই। এখন cargo test চালান:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 1.31s
Running unittests src/lib.rs (target/debug/deps/adder-1082c4b063a8fbe6)
running 1 test
test tests::internal ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/integration_test.rs (target/debug/deps/integration_test-1082c4b063a8fbe6)
running 1 test
test it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
output-এর তিনটি অংশে আছে unit test, integration test, আর doc test। খেয়াল করুন, কোনো অংশের কোনো test fail করলে পরের অংশগুলো চলবে না। যেমন, একটি unit test fail করলে integration ও doc test-এর কোনো output থাকবে না, কারণ ওই test-গুলো চলে কেবল তখনই, যখন সব unit test pass করে।
unit test-এর প্রথম অংশটি আগে যেমন দেখেছি তেমনই: প্রতিটি unit test-এর জন্য একটি লাইন (Listing 11-12-এ যোগ করা internal
নামের একটি), তারপর unit test-গুলোর একটি সারসংক্ষেপ লাইন।
integration test-এর অংশটি শুরু হয় Running tests/integration_test.rs লাইন দিয়ে। এরপর সেই integration test-এর প্রতিটি test function-এর জন্য একটি লাইন, আর Doc-tests adder অংশ শুরু হওয়ার ঠিক আগে integration test-এর ফলের একটি সারসংক্ষেপ লাইন থাকে।
প্রতিটি integration test ফাইলের নিজস্ব অংশ থাকে, তাই tests directory-তে আরও ফাইল যোগ করলে আরও integration test অংশ দেখা যাবে।
test function-এর নাম cargo test-এ argument হিসেবে দিয়ে এখনো নির্দিষ্ট একটি integration test function চালানো যায়। নির্দিষ্ট
একটি integration test ফাইলের সব test চালাতে cargo test-এর --test argument-এর পরে ফাইলের নাম দিন:
$ cargo test --test integration_test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.64s
Running tests/integration_test.rs (target/debug/deps/integration_test-82e7799c1bc62298)
running 1 test
test it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
এই command শুধু tests/integration_test.rs ফাইলের test-গুলো চালায়।
integration test-এ submodule
আরও integration test যোগ করতে থাকলে সেগুলো গুছিয়ে রাখতে হয়তো tests directory-তে আরও ফাইল বানাতে চাইবেন; যেমন, test function-গুলোকে তারা কোন কার্যক্ষমতা test করছে সেই অনুযায়ী ভাগ করতে পারেন। আগেই বলা হয়েছে, tests directory-র প্রতিটি ফাইল নিজস্ব আলাদা crate হিসেবে compile হয়, যা আলাদা scope তৈরি করে শেষ ব্যবহারকারীরা আপনার crate যেভাবে ব্যবহার করবে তা আরও কাছ থেকে অনুকরণ করতে কাজে লাগে। তবে এর মানে হলো, অধ্যায় 7-এ কোডকে module ও ফাইলে ভাগ করা নিয়ে যা শিখেছেন, tests directory-র ফাইলগুলো src-এর ফাইলগুলোর মতো আচরণ করে না।
tests directory-র ফাইলগুলোর এই ভিন্ন আচরণ সবচেয়ে বেশি চোখে পড়ে যখন একাধিক integration test ফাইলে ব্যবহারের জন্য কিছু
সহায়ক function থাকে, আর আপনি অধ্যায় 7-এর “module-গুলোকে আলাদা ফাইলে ভাগ
করা” অংশের ধাপগুলো মেনে সেগুলোকে একটি সাধারণ module-এ বের করে আনার চেষ্টা করেন।
যেমন, যদি tests/common.rs তৈরি করে তাতে setup নামের একটি function রাখি, তাহলে setup-এ এমন কিছু কোড যোগ করতে পারি যা
একাধিক test ফাইলের একাধিক test function থেকে call করতে চাই:
Filename: tests/common.rs
pub fn setup() {
// আপনার library-র test-এর জন্য নির্দিষ্ট প্রস্তুতির কোড এখানে বসবে
}
আবার test চালালে test output-এ common.rs ফাইলের জন্য একটি নতুন অংশ দেখব, যদিও এই ফাইলে কোনো test function নেই এবং আমরা
কোথাও থেকে setup function call-ও করিনি:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.89s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test tests::internal ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/common.rs (target/debug/deps/common-92948b65e88960b4)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/integration_test.rs (target/debug/deps/integration_test-92948b65e88960b4)
running 1 test
test it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
test-এর ফলে common-এর পাশে running 0 tests দেখানো আমরা চাইনি। আমরা শুধু অন্য integration test ফাইলগুলোর সঙ্গে কিছু কোড
ভাগ করতে চেয়েছিলাম। test output-এ common যাতে না দেখায়, সেজন্য tests/common.rs তৈরি না করে আমরা tests/common/mod.rs
তৈরি করব। project directory এখন দেখতে এমন:
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
└── tests
├── common
│ └── mod.rs
└── integration_test.rs
এটি পুরোনো নামকরণের রীতি, যা Rust-ও বোঝে এবং যার কথা আমরা অধ্যায় 7-এর “বিকল্প ফাইল path” অংশে
বলেছিলাম। এভাবে ফাইলের নাম দিলে Rust common module-কে integration test ফাইল হিসেবে গণ্য করে না। setup function-এর কোড
tests/common/mod.rs-এ সরিয়ে tests/common.rs ফাইলটি মুছে দিলে test output-এর সেই অংশটি আর দেখা যাবে না। tests
directory-র subdirectory-তে থাকা ফাইলগুলো আলাদা crate হিসেবে compile হয় না, আর test output-এ তাদের কোনো অংশও থাকে না।
tests/common/mod.rs তৈরি করার পর যেকোনো integration test ফাইল থেকে এটিকে একটি module হিসেবে ব্যবহার করা যায়। এখানে
tests/integration_test.rs-এর it_adds_two test থেকে setup function call করার একটি উদাহরণ:
Filename: tests/integration_test.rs
use adder::add_two;
mod common;
#[test]
fn it_adds_two() {
common::setup();
let result = add_two(2);
assert_eq!(result, 4);
}
খেয়াল করুন, mod common; ঘোষণাটি Listing 7-21-এ দেখানো module ঘোষণার মতোই। তারপর test function-এ আমরা common::setup()
function call করতে পারি।
binary crate-এর integration test
আমাদের project যদি এমন একটি binary crate হয় যাতে শুধু একটি src/main.rs ফাইল আছে এবং কোনো src/lib.rs ফাইল নেই, তাহলে
tests directory-তে integration test তৈরি করে use statement দিয়ে src/main.rs ফাইলে সংজ্ঞায়িত function scope-এ আনা যায়
না। শুধু library crate-ই এমন function প্রকাশ করে যা অন্য crate ব্যবহার করতে পারে; binary crate তৈরি হয় নিজে নিজে চলার জন্য।
এ কারণেই binary দেয় এমন Rust project-গুলোতে একটি সোজাসাপ্টা src/main.rs ফাইল থাকে, যা src/lib.rs ফাইলে থাকা logic call
করে। এই গঠন ব্যবহার করলে integration test use দিয়ে গুরুত্বপূর্ণ কার্যক্ষমতা হাতের নাগালে এনে library crate test করতে পারে।
গুরুত্বপূর্ণ কার্যক্ষমতা কাজ করলে src/main.rs ফাইলের অল্প কোডটুকুও কাজ করবে, আর সেই অল্প কোড test করার দরকার নেই।
সারসংক্ষেপ
Rust-এর testing বৈশিষ্ট্যগুলো কোড কীভাবে কাজ করা উচিত তা নির্দিষ্ট করার একটি উপায় দেয়, যাতে পরিবর্তন আনার পরও কোড আপনার প্রত্যাশামতো কাজ করে যায়। unit test একটি library-র বিভিন্ন অংশ আলাদাভাবে পরীক্ষা করে এবং private implementation-এর খুঁটিনাটিও test করতে পারে। integration test যাচাই করে library-র অনেক অংশ একসঙ্গে ঠিকমতো কাজ করে কি না, আর বাইরের কোড যেভাবে library ব্যবহার করবে সেভাবেই library-র public API দিয়ে কোড test করে। Rust-এর type system ও ownership-এর নিয়ম কিছু ধরনের বাগ ঠেকাতে সাহায্য করলেও, আপনার কোড কেমন আচরণ করবে বলে আশা করা হয় সে সংক্রান্ত logic-এর বাগ কমাতে test এখনো গুরুত্বপূর্ণ।
চলুন এই অধ্যায়ে ও আগের অধ্যায়গুলোতে শেখা জ্ঞান একসঙ্গে কাজে লাগিয়ে একটি project-এ কাজ করি!