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);