代码改变世界

Convert MSTest to Nunit

2010-10-14 20:24  敏捷的水  阅读(2524)  评论(1编辑  收藏  举报

今天想在自动集成服务器上用MSTest,但遗憾的是MSTest.exe不能使用目录,命名空间等,因此需要将MSTest转换为Nunit1.

1. 首先移除MSTest的程序集,引入Nunit.Framework.dll

2.然后使用如下Ruby脚本.

all_files= Dir[File.join("D:", "UnitTest/**/**/**")];
all_files.each { |x|
  if x.to_s().include?(".cs")
    begin
    puts x.to_s
    f = File.open(x.to_s, "r+")
    content=""
    f.each { |line| content+=line }

   s=content.gsub(/\[TestMethod\]/, "[Test]").gsub(/\[TestMethod\(\)\]/, "[Test]").
            gsub(/\[TestClass\]/, "[TestFixture]").gsub(/\[TestClass\(\)\]/, "[TestFixture]").
            gsub(/\[ClassInitialize\]/, "[TestFixtureSetUp]").gsub(/\[ClassInitialize\(\)\]/, "[TestFixtureSetUp]").
            gsub(/\[ClassCleanup\]/, "[TestFixtureTearDown]").gsub(/\[ClassCleanup\(\)\]/, "[TestFixtureTearDown]").
            gsub(/\[TestInitialize\]/, "[SetUp]").gsub(/\[TestInitialize\(\)\]/, "[SetUp]").
            gsub(/\[TestCleanUp\]/, "[TearDown]").gsub(/\[TestCleanUp\(\)\]/, "[TearDown]").
            gsub(/\[AssemblyInitialize\]/, ""). gsub(/\[AssemblyInitialize\(\)\]/, "").
            gsub(/\[AssemblyCleanUp\]/, "").gsub(/\[AssemblyCleanUp\(\)\]/, "").
            gsub(/namespace\s+?/,"using NUnit.Framework;\n\nnamespace ")
    f.close
    File.delete(x)
    new= File.new(x, "w+")
    new.puts s
    new.close
    s=""
       rescue =>e
    end
  end
}
3. 移除MSTest的部分特性的东西,如TestContext等,这个应该很少了
然后,就顺利转换成功了