SUT无法在编码测试UI中保存文件

时间:2015-03-16 10:03:39

标签: c# winforms coded-ui-tests

我是Visual Studio 2013中编码ui测试的新手。

我试图测试读取文件,执行操作和保存该文件的程序的一部分。通过手动测试,没有问题。使用编码的ui测试,程序可以读取文件但无法保存。

有一个非托管异常" System.IO.IOException:进程无法访问文件' ... \ tempData.xml',因为它正由另一个进程使用。&# 34;

当我点击"继续"在异常框中,我重新单击保存文件的按钮,没有更多异常,文件已保存。非常奇怪。

好像我的测试在SUT使用时访问了文件

我的测试:

        [TestInitialize]
        public void RunApp()
        {
            _sut = ApplicationUnderTest.Launch(Settings.Default.pathSut);
        }


        [TestMethod]
        public void CompleteExport()
        {
            this.UiMap.ExportDataComplete(); //Read and save the file
            this.UIMap.AssertSuccessExport();
        }

我保存文件的方法:

public void TrySave()
{

string executableFolderPath = Environment.CurrentDirectory;
string path = executableFolderPath + "\\" + Resources.xmlFile;
bool result = this.ValidateXml();
if (result)
    XmlFic.Save(path);
}

我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我的坏:

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);

我忘了:

reader.Close();

奇怪的行为,为什么它在手动测试中工作?也许GC有时间处理它,并没有时间进行自动化测试?问题解决了,但如果有人有解释......

由于