如何在 C#/ASP.NET 网站上使用 NUnit GUI?

发布于 2024-09-02 15:28:24 字数 282 浏览 3 评论 0原文

我有一个 C#/ASP.NET 网站,App_Code 目录中有一些代码 (*.cs) 文件。我想使用 NUnit 测试它们。我已经编写了一个带有正确的 [TestFixture] 和 [Test] 注释的测试文件,并将其放在这里:App_Code/Test/TestClassName.cs。

我加载 NUnit GUI 来运行它,但它要求我选择 .exe 或 .dll 文件。我的项目的bin文件夹中没有。我的项目确实成功运行并构建了一切,但仍然没有 exe 或 dll 文件。我怎样才能让 NUnit Gui 在该类中运行测试?

I have a C#/ASP.NET website that has some code (*.cs) files in the App_Code directory. I would like to test them using NUnit. I have written a test file with the proper [TestFixture] and [Test] annotations and have put it here: App_Code/Test/TestClassName.cs.

I load up the NUnit GUI to run it but it wants me to select a .exe or .dll file. There is none in the bin folder of my project. My project does successfully run and is built and everything, but still no exe or dll file. How can I get the NUnit Gui to just run the test in that class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哎呦我呸! 2024-09-09 15:28:25
  1. 首先,您要为测试创建一个新项目。如果您碰巧有任何内部类或属性,则应使用 InternalsVisibleToAttribute 以便能够在“真实”项目之外的测试项目中测试这些内容。
    此属性适用于整个程序集,因此我建议将其放入“真实”程序集的 Assembly.info 文件中。

  2. 在您的测试项目中,添加对“真实”程序集的引用。

  3. 确保准确地知道二进制文件 (Assembly.dll) 的路径;

  4. 从 NUnit GUI 中打开 TestsProjectAssembly.dll,确保浏览到正确的文件夹;

  5. 您可能还希望 NUnit GUI 在每次测试运行时重新加载程序集(选项属性中有一个选项可以执行此操作);

  6. 运行您的测试。

绝对确保您的路径或可浏览文件夹是生成测试项目的路径或可浏览文件夹。

  1. First, you want to create a new project for your tests. If you happen to have any internal classes or attributes, you should use InternalsVisibleToAttribute in order to be able to test these from within your testing project, outside your "real" project.
    This attribute is suitable for the entire assembly, so I recommend putting it into your Assembly.info file of your "real" assembly.

  2. From within your tests project, add a reference to your "real" assembly.

  3. Make sure to exactly know the path to your binary (assembly.dll);

  4. Open your TestsProjectAssembly.dll from within your NUnit GUI, makeing sure you are browsing to the right folder;

  5. You might also want NUnit GUI to reload your assembly on each tests run (there is an option for doing so in the options properties);

  6. Run your tests.

Absolutely make sure your path or browsable folder is the one in which your testing project is generated.

本王不退位尔等都是臣 2024-09-09 15:28:24

我不建议将测试代码放在要部署到生产的同一个包中。

您可能希望将测试类移动到库项目中,例如 Business.UnitTest(可能有一种内置方法来创建 nUnit 特定项目,如果有,请使用它)。然后将 App_Code 目录中的业务类移动到另一个名为 Business 的项目中。让 Business.UnitTest 引用业务库。这应该让 nUnit 运行(我不相信 nUnit 可以针对网站运行,只能针对库运行,但我不能 100% 确定)。

为您的网站添加对刚刚创建的业务库的引用,以便您的生产代码可以访问业务库中的业务对象。您可能必须解决一些名称空间问题,但这应该不会太糟糕。

I don't recommend putting test code in the same package you'll be deploying to production.

You may want to move the test classes to a library project, something like Business.UnitTest(there may be a built in way to create an nUnit specific project, if there is, use that). Then move the business classes that are in your App_Code directory into another project called Business. Have Business.UnitTest reference the Business library. That should get nUnit to run(I don't believe that nUnit runs against websites, only libraries, but I'm not 100% sure).

For your website add a reference to the business library you just created so your production code can access the business objects in the business library. You may have to work out some namespace issues, but that shouldn't be too bad.

獨角戲 2024-09-09 15:28:24

.NET 网站项目的技巧在于,代码文件通常不会预先编译,而是在执行相应页面时进行编译。这在测试方面提出了挑战,因为正如您提到的,NUnit 希望运行 .exe.dll 进行测试。

处理该问题的一种方法是将网站项目转换为网络应用程序;它们听起来相似,但工作方式不同。与不需要预先编译的网站相比,网络应用程序需要它。因此,您将拥有一个或多个编译为程序集 (.dll) 或可执行文件 (.exe) 的项目。然后,NUnit 可以连接到这些来运行测试。

为了实现这一点,您需要将可测试的代码分离到另一个项目中;您的前端 Web 应用程序可以引用另一个项目来使用其中的代码。理想情况下,前端将是逻辑和用户交互的薄层,真正的工作可以发送到第二个项目。因此,第二个项目就是您想要测试的项目。

您将希望再有一个项目来包含测试 - 一般来说,不要将测试与正在测试的代码放在同一个项目中。该项目引用正在测试的项目和 NUnit,并包含测试本身。该程序集是您指示 NUnit 运行以进行测试的程序集。

The trick with .NET Website projects is that the code files are not normally compiled up front, they are compiled on execution of the respective pages. This presents a challenge where testing is concerned, since as you mentioned NUnit wants to run a .exe or .dll for testing.

One way to deal with the issue is to convert the website project to a web application; they sound similar, but work in different ways. In contrast to a website not requiring up-front compilation, a web application requires it. So you would have one or more projects that compile to assemblies (.dll) or executables (.exe). NUnit could then hook into those to run the tests.

To make this work, you would want to separate testable code into another project; your front-end web application can refer to this other project to make use of the code within. Ideally, the front-end would be a thin layer of logic and user interaction, and the real work can be sent to the second project. Therefore, the second project is what you will want to test.

You'll want to have one more project to contain the tests - general wisdom is to not have your tests in the same project as the code being tested. This project refers to the project being tested, and to NUnit, and contains the tests themselves. This assembly is what you would direct NUnit to run for testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文