关于 testunit-skill
为使用 Test::Unit 编写和运行 Ruby 测试提供参考,面向 Ruby 开发者,涵盖测试结构、断言、测试夹具和测试发现。
主要功能
- 01Test::Unit 测试类模式
- 02常用断言方法
- 03类级启动与关闭夹具
- 04测试命名与顺序指南
- 05运行和自动发现命令
适用场景
- 01为 Ruby 类编写测试
- 02在测试类中共享初始化流程
- 03运行测试文件或发现测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill testunit-skill安装后在 Claude Code 中输入/testunit-skill确认已加载。
附带文件reference/advanced-patterns.mdreference/playbook.md
详细介绍
Test::Unit Ruby 技能
核心模式
基本测试
require 'test/unit'
class TestCalculator < Test::Unit::TestCase
def setup
@calc = Calculator.new
end
def test_addition
assert_equal(5, @calc.add(2, 3), "2 + 3 should equal 5")
end
def test_subtraction
assert_equal(1, @calc.subtract(3, 2))
end
def test_division_by_zero
assert_raise(ZeroDivisionError) { @calc.divide(10, 0) }
end
def test_float_division
assert_in_delta(3.33, @calc.divide(10, 3), 0.01)
end
def teardown
# cleanup
end
end
断言
assert_equal(expected, actual, message = nil)
assert_not_equal(unexpected, actual)
assert_true(value)
assert_false(value)
assert_nil(value)
assert_not_nil(value)
assert_instance_of(klass, obj)
assert_kind_of(klass, obj)
assert_respond_to(obj, :method_name)
assert_match(/pattern/, string)
assert_no_match(/pattern/, string)
assert_include(collection, item)
assert_not_include(collection, item)
assert_empty(collection)
assert_not_empty(collection)
assert_raise(ErrorClass) { block }
assert_nothing_raised { block }
assert_in_delta(expected, actual, delta)
assert_operator(value, :>, 5)
assert_send([obj, :method, arg])
assert_block("message") { condition }
测试夹具
class TestDatabase < Test::Unit::TestCase
def self.startup
# Once before all tests in class
@@db = Database.connect('test')
end
def self.shutdown
# Once after all tests in class
@@db.disconnect
end
def setup
# Before each test
@@db.begin_transaction
end
def teardown
# After each test
@@db.rollback
end
end
测试排序与命名
class TestUserService < Test::Unit::TestCase
# Methods must start with 'test_'
def test_create_user
user = UserService.create('Alice', '[email protected]')
assert_not_nil(user)
assert_equal('Alice', user.name)
end
def test_find_user_by_email
user = UserService.find_by_email('[email protected]')
assert_instance_of(User, user)
end
end
安装:gem install test-unit
运行:ruby test/test_calculator.rb 或 ruby -Itest -e "Dir.glob('test/**/test_*.rb').each{|f| require f}"
自动发现:testrb test/(需要 test-unit gem)
深入模式
有关高级模式、调试指南、CI/CD 集成和最佳实践,
请参阅 reference/playbook.md。
评论
支持 Markdown