如何找到非exe程序的C#入口点?

时间:2014-05-05 18:39:27

标签: c# entry-point

我读到的有关C#入口点类的所有信息都涉及:

static int Main(string[] args)

因为这是特定于EXE程序的。

但我的程序是C#自动化测试框架(解决方案中的.cs),使用Specflow功能文件和步骤定义设计。

我现在添加了一个名为Program.cs - Int Main类的(入口点)类,但我注意到在运行自动化测试时不会调用此入口点类。很可能是因为我的程序不是EXE程序而是测试框架。

如何找到Non exe程序的C#入口点?

因为我想使用我每次运行测试时会调用的一个类的“报告”代码:

namespace Project.Core
{
    public class Program
    {
        public static int Main(string[] args)
        {
            Adapter.DefaultSearchTimeout = 5000;

            int error;
            try
            {
                error = TestSuiteRunner.Run(typeof (Program), Environment.CommandLine);
            }
            catch (Exception e)
            {
                TestReport.Setup(ReportLevel.Debug, "myReport.rxlog", true);
                Report.Screenshot();
                throw new ApplicationException("Error Found", e);
            }
            return error;
        }
    }
}

2 个答案:

答案 0 :(得分:4)

非exe项目(即DLL)没有入口点。它们是由其他进程调用的API,它们有自己的入口点。

你应该在测试之前研究适当的""您与SpecFlow一起使用的测试框架的方法。例如,在MSTest中,此类代码将使用签名为:

的方法
[TestInitialize]
public void TestInitialize()
{
    // Your code
}

MSDN Documentation.

答案 1 :(得分:0)

由于我的项目同时使用MsTest和Ranorex API,我无法确定在测试运行之前/期间初始化项目的哪个方面。所以我决定将Try / Catch代码结构直接添加到我项目中的每个Step Defintion方法中。

Try()
{
    // code
}
Catch (Exception e)
{
    TestReport.Setup(ReportLevel.Debug, "myReport.rxlog", true);
    Report.Screenshot();
    throw new ApplicationException("Error Found", e);
} 

我可以轻松地将Catch代码移动到辅助类中,这样我只有一个代码实例通过我的项目使用:

catch (Exception)
{
    WpfHelpers.ExtensionMethods.CatchException(null);
}

助手类:

public static class ExtensionMethods
{
    public static void CatchException(SystemException e)
        {
            TestReport.Setup(ReportLevel.Debug, "myReport.rxlog", true);
            Report.Screenshot();
            throw new ApplicationException("Error Found", e);
        }
}

请注意,我正在使用此结构,以便在发生故障时可以使用Ranorex的Report.Screenshot功能。