Rustのテストフレームワークを作った(ので使って)
どうもnasaです。
Rust製のテストフレームワークRuspecを作りました。ので良かったら使ってみてください。
GitHub - k-nasa/ruspec: write like Rspec testing framework with rust
Rustでも、RubyのRspecのようにdescribe,context,itなどのキーワードでテストを記述したかったのですが、自分好みのものが見当たらなかったので作りました。 (あるにはあったのですが、書き方がイケてなかったり、構文拡張を使っていたり(Rustの構文拡張は仕様が安定していない)したので、、、)
文法はこんな感じです。
use ruspec::ruspec; ruspec! { describe "test module name" { before { let context = 5; } subject { context + 5 } it "test name" { assert_eq!(subject, 10); } } describe "test module 2" { before { let context = 5; } it "test name" { assert_eq!(context, 5); } context "context is 6" { before { let context = 6; } it "should equal 6" { assert_eq!(context, 6); } } } }
このように書くと下のコードに展開されます
mod test_module_name { #[test] fn test_name() { let context = 5; assert_eq(context + 5, 10) } } mod test_module_2 { #[test] fn test_name() { let context = 5; assert_eq(context, 10) } mod context_is_6 { #[test] fn should_equal_6() { let context = 6; assert_eq!(context, 6) } } }
今ある機能 - beforeによる前処理の記述 - afterによる後処理の記述 - subjectによるテスト対象のまとめ
これらが今ある機能です。
できないこと - #[should_panic]をつけて失敗することを期待するテストはまだ未対応 - #[ignore]も同じく未対応。 - subjectは呼び出し時に展開されるわけでなく、beforeの後に変数にしている
attributeをつけるようなケースにはまだ未対応となっています。
また、subjectはbeforeの後に let subject = hoge
というように変数に値を入れているだけなので、assert!(subject)
と書いても、このタイミングで展開されるわけではないです。(イケてないけど、早く作りたかった、、、PR待ってるで)
よかったら使ってみてください。 まだまだイケてないツールですがfeature requestなどくれれば頑張って開発していきます!