如何生成带有类中方法名称的文件? [英] How to generate a file with the names of the methods in class?

查看:40
本文介绍了如何生成带有类中方法名称的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有这个单元测试文件:


I have this Unit Test file:

class WebsiteCheckPageElements < Test::Unit::TestCase

def setup
 ...
end

def test_achievements_iknow_report_page
  ...
end

def test_achievements_daily_report_page
  ...
end

def test_client_side_report_page
  ...
end
end

我需要编写一个 ruby​​ 脚本,将测试名称写入 .txt 文件.就像是

I need to write a ruby script that will write the names of the tests to .txt file. Something like

设置
test_achievements_iknow_report_page
test_achievements_daily_report_page
test_client_side_report_page

setup
test_achievements_iknow_report_page
test_achievements_daily_report_page
test_client_side_report_page

预先感谢您的建议.

推荐答案

您可以使用一个简单的脚本,该脚本使用正则表达式打印与 def 匹配的行:

You could use a simple script that uses regular expressions to print the lines that match def:

match_test_methods.rb

File.open('your_unit_test_file.rb').each_line do |line|
  match = line.match(/def (.+)/)
  puts match[1] if match
end

然后把脚本的结果放在一个.txt文件中

Then put the result of the script in a .txt file

ruby match_test_methods.rb > test_methods.txt

这篇关于如何生成带有类中方法名称的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆