Automated tests 在运行Visual Studio的自定义测试类型时解决FileNotFound问题

Automated tests 在运行Visual Studio的自定义测试类型时解决FileNotFound问题,automated-tests,vs-extensibility,visual-studio-test-runner,Automated Tests,Vs Extensibility,Visual Studio Test Runner,我已经为VisualStudio实现了一个自定义测试类型。自定义测试类型从DLL读取其测试元素。我的ITip实现工作得很好。测试元素将被加载并显示在“测试视图”工具窗口中 当我选择测试元素并运行它们时,它们最终处于未执行状态。调试此问题时,我发现从QTAgent32.exe引发了FileNotFoundException。它告诉我它找不到定义测试用例的dll。此外,它在调用TestAdapter.Initialize方法之前失败。我将测试dll复制到Visual studio的PrivateSa

我已经为VisualStudio实现了一个自定义测试类型。自定义测试类型从DLL读取其测试元素。我的ITip实现工作得很好。测试元素将被加载并显示在“测试视图”工具窗口中

当我选择测试元素并运行它们时,它们最终处于未执行状态。调试此问题时,我发现从QTAgent32.exe引发了FileNotFoundException。它告诉我它找不到定义测试用例的dll。此外,它在调用TestAdapter.Initialize方法之前失败。我将测试dll复制到Visual studio的PrivateSassemblies目录。当我这样做时,我的测试元素通过了。我还可以在自定义测试适配器中调试代码。因此,所有这些的意思是QTAgent32.exe无法在其原始目录中找到我的测试dll

我的问题是,我应该怎么做才能让QTAgent32在原始目录中找到我的测试dll?对于completenes,我添加了我的Tip Load方法代码:

public override ICollection Load(string location, ProjectData projectData, IWarningHandler warningHandler)
{
    Trace.WriteLine("Started RegexTestTip Load.");
    if (string.IsNullOrEmpty(location))
    {
        throw new ArgumentException("File location was not specified!", "location");
    }

    var fileInfo = new FileInfo(location);
    if (!fileInfo.Exists)
    {
        throw new ErrorReadingStorageException(
                string.Format("Could not find a file on the specified location: {0}", location));
    }
    var result = new List<ITestElement>();
    var extension = fileInfo.Extension.ToLower();
    if (extension != ".dll")
    {
        return result;
    }

    Assembly testAssembly = Assembly.LoadFrom(location);
    var testClasses = testAssembly.GetTypes().
        Where(t => Attribute.IsDefined(t, typeof(RegexTestClassAttribute)));

    foreach (Type testClass in testClasses)
    {
        PropertyInfo property = testClass.GetProperties().
            SingleOrDefault(p => Attribute.IsDefined(p, typeof(TestedRegexAttribute)));
        if (property == null || !TestedRegexAttribute.Validate(property))
        {
            throw new InvalidDataInStorageException("A Regex test must define a Tested Regex property with type Regex");
        }
        var testCases = testClass.GetProperties().
            Where(p => Attribute.IsDefined(p, typeof(RegexTestCaseAttribute)));

        foreach (PropertyInfo testCase in testCases)
        {
            if (!RegexTestCaseAttribute.Validate(testCase))
            {
                throw new InvalidDataInStorageException("A test case property must return a String value.");
            }
            var testElement = new RegexTestElement(property, testCase);
            testElement.Storage = location;
            testElement.Name = testCase.Name;
            testElement.Description = "A simple description";
            testElement.ProjectData = projectData;
            result.Add(testElement);
        }
    }
    Trace.WriteLine("Finished RegexTestTip Load.");
    return result;
}
公共覆盖ICollection加载(字符串位置、ProjectData ProjectData、IWarningHandler警告Handler)
{
WriteLine(“已启动RegexTestTip加载”);
if(string.IsNullOrEmpty(位置))
{
抛出新ArgumentException(“未指定文件位置!”,“位置”);
}
var fileInfo=新的fileInfo(位置);
如果(!fileInfo.Exists)
{
抛出新的ErrorReadingStorage异常(
Format(“无法在指定位置找到文件:{0}”,位置));
}
var result=新列表();
var extension=fileInfo.extension.ToLower();
if(扩展名!=“.dll”)
{
返回结果;
}
Assembly testAssembly=Assembly.LoadFrom(位置);
var testClasses=testAssembly.GetTypes()。
其中(t=>Attribute.IsDefined(t,typeof(RegexTestClassAttribute));
foreach(在testClasses中键入testClass)
{
PropertyInfo属性=testClass.GetProperties()。
SingleOrDefault(p=>Attribute.IsDefined(p,typeof(testedregaxattribute));
if(property==null | |!testedregaxattribute.Validate(property))
{
抛出新的InvalidDataInStorageException(“正则表达式测试必须定义类型为Regex的已测试正则表达式属性”);
}
var testCases=testClass.GetProperties()。
其中(p=>Attribute.IsDefined(p,typeof(RegexTestCaseAttribute));
foreach(testCases中的PropertyInfo testCase)
{
如果(!RegexTestCaseAttribute.Validate(testCase))
{
抛出新的InvalidDataInStorageException(“测试用例属性必须返回字符串值”);
}
var testElement=新的RegexTestElement(属性,testCase);
testElement.存储=位置;
testElement.Name=testCase.Name;
testElement.Description=“简单描述”;
testElement.ProjectData=项目数据;
结果。添加(testElement);
}
}
WriteLine(“完成的RegexTestTip加载”);
返回结果;
}

您是否尝试将dll与可执行文件放在同一目录中?请原谅这一点的明显性,但有时是真正简单的事情咬着我们。但它在GAC中,对吗?

就像我在问题中说的,我做到了。我把我的dll放在Visual studio的PrivateSassemblies目录中,它解决了这个问题。但这不是一个解决方案,因为您没有将每个单元测试dll复制到VisualStudio的PrivateSassemblies目录。有些事情需要尝试-您是否停止并启动了代理?也许它需要根据这个链接完全重新启动,因为它是不正常的:它没有说明你的具体问题,但我想知道这里是否有相关的东西。。。您可以使用“sc\\mymachine stop vsttagent”和“sc\\mymachine start vsttagent”抱歉,我不认为这是相关的,因为我是从可视化研究而不是从TFS运行测试。这是正确的,正如我提到的,我肯定会将其作为注释而不是答案,但它不是答案。但幸运的是,今天早上我发现自己在声誉方面越过了评论的障碍。我希望你能找到解决这个问题的办法。Rgds…总是突出问题以快速理解要点:)@Venkat-突出是什么意思?大胆一点?