C# 中比较DOC内容的测试代码

上一篇 / 下一篇  2012-01-13 14:53:16

公共测试
 
using System;
using System.Text;
using System.Data;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Office.Interop.Word;
using System.Collections.Generic;
using System.ComponentModel;
namespace DocTest
{
    [TestClass]
    public class UnitTest_CompareDocMethod
    {
        [TestMethod]
        public static bool CompareDoc(String sourceFile,String targetFile)
        {
            //此处把sourceFile和targetFile当做参数从之前的脚步中生成后传过来
            //string sourceFile = "E:\\DocBaseLineFile\\WordDoc.doc";
            //string targetFile = "E:\\DOCTestResult\\WordDoc.doc";
            WordHelper helper = new WordHelper(sourceFile);
           
            if (helper.Compare(targetFile))
            {
                helper.ReacceptRevised();
                return false;
                //Assert.AreEqual(sourceFile ,targetFile);
                //MessageBox.Show("Two document not same!");
            }
            else
            {
                helper.ReacceptRevised();
                return true;
                //Assert.IsFalse(false,message);
                //MessageBox.Show("Two document same!");
            }
           
        }
    }
       
    public class WordHelper
    {
        private Microsoft.Office.Interop.Word.Application _wordApplication;
        private Microsoft.Office.Interop.Word.Document _wordDocument;
        object nullobj = System.Reflection.Missing.Value;
        public WordHelper(string fileName)
        {
            this._wordApplication = new Microsoft.Office.Interop.Word.Application();
            object bjFile = fileName;
            this._wordDocument = _wordApplication.Documents.Open(ref objFile, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
        }
         public bool Compare(string file)
        {
            bool hasChanged = false;
            object compareTarget = Microsoft.Office.Interop.Word.WdCompareTarget.wdCompareTargetCurrent;
            object addToRecentFiles = false;
            _wordDocument.Compare(file, ref nullobj, ref compareTarget, ref nullobj, ref nullobj, ref addToRecentFiles, ref nullobj, ref nullobj);
            object bjfile = file;
           
            //this._wordDocument = _wordApplication.Documents.Open(ref objfile, ref nullobj, ref nullobj,
            //   ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
            //   ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            Revision r = _wordApplication.Selection.NextRevision(ref nullobj);
            if (r != null)
                hasChanged = true;
            _wordApplication.NormalTemplate.Saved = true;
            //_wordDocument.Save();
            //object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            //_wordApplication.Quit(ref saveOption, ref nullobj, ref nullobj);
            return hasChanged;
        }
       
        public void ReacceptRevised()
        {
            //_wordDocument.AcceptAllRevisions();
            //_wordDocument.Save();
            object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            _wordApplication.Quit(ref saveOption, ref nullobj, ref nullobj);
        }          
           
        }
    }
 
 
 
 
然后在测试主程序中调用并添加判断
Assert.AreEqual(UnitTest_CompareDocMethod.CompareDoc(BaseLineFile, outputFile), true);
 

TAG: API测试

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-01  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 865
  • 日志数: 2
  • 建立时间: 2011-01-18
  • 更新时间: 2012-01-13

RSS订阅

Open Toolbar