如何在具有特定权限的用户下运行一个 NUnit 测试

发布于 2024-10-08 11:00:36 字数 49 浏览 4 评论 0原文

是否可以在特定用户帐户下运行测试套件中的测试之一?我需要以某种方式模拟用户权限问题。

Is it possible to run one of the tests in a testsuite under a specific user account? I need to simulate somehow a user privileges issue.

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

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

发布评论

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

评论(2

你怎么敢 2024-10-15 11:00:36
runas.exe /user:DOMAIN\someUser "nunit-console.exe somelibrary.dll"
runas.exe /user:DOMAIN\someUser "nunit-console.exe somelibrary.dll"
独夜无伴 2024-10-15 11:00:36

我已将要点合并为一个答案,并概述了所涉及的步骤。

我还添加了有关使用新的 NUnit 3“NUnit-Lite”单 exe 测试+运行程序以及使用 Windows 任务计划程序作为在用户帐户下运行测试的简单机制的信息。

达林·迪米特洛夫(Darin Dimitrov)的答案是总体上关于金钱,但评论中隐藏了一些精彩的观点,有些人可能需要更多有关如何设置的指导。另外,当他写下答案时,NUnit-Lite 还不可用。

第 1 部分:将需要在特定用户帐户下运行的测试放入其自己的单独程序集中。大致步骤:

  • 例如,在 Visual Studio 中,您可以选择“文件/新建项目”,然后选择 NUnit 项目模板(在 Framework 或 .NET Core 中,视情况而定),为项目命名适当的名称。

  • 将涉及的特定测试的代码移至您刚刚创建的新程序集中。获取要生成和运行的程序集,并使用您在 Visual Studio 中登录的帐户给出预期结果。暂时不用担心其他用户帐户。

  • 选项:使用 NUnit-Lite 单 EXE 测试运行程序打包方法,这是 NUnit3 中的新增功能,如下所述:https://docs.nunit.org/articles/nunit/running-tests/NUnitLite-Runner.html

第 2 部分:将测试程序集部署到要运行的位置它,以便能够使用 nunit-console.exe 程序在程序集中运行测试。

  • 有关运行时要求,请参阅 NUnit 文档。

第 3 部分:设置在特定用户帐户下执行 nunit-console 或 NUnit-Lite 单个 EXE。

  • 选项 1:使用“runas”方法
    runas.exe /user:DOMAIN\someUser "nunit-console.exe somelibrary.dll"
    或者
    runas.exe /user:DOMAIN\someUser "MyTestsAndNUnitLiteRunner.exe"

  • 我相信您会启动命令提示符;在 Windows 10 中,它将是“开始”/(键入 CMD),然后右键单击“命令提示符”选项,选择“以不同用户身份运行”,然后输入特定用户的用户 ID 和凭据。

  • 在某些版本的 Windows 10 中,您不会看到“以不同用户身份运行”选项。如果发生这种情况,请参阅本文:https://superuser.com/questions/1045158/how-do-you-run-as-a- Different-user-from-the-start-menu-in-windows-10


  • 选项 2:使用 Windows 任务计划程序设置在所需用户帐户下运行的作业。在任务设置过程中,您将需要用户帐户登录名和凭据。一旦计划任务设置完毕,您只需右键单击它并选择“运行”即可立即运行它。命令行将与此类似:
    “nunit-console.exe”somelibrary.dll
    或者,对于 NUnit-Lite:
    “MyTestsAndNUnitLiteRunner.exe”

在任务设置中,您可能需要使用文件夹名称,并且如果所涉及的路径中包含空格,则需要使用引号。

第 4 部分:使用您选择的方法运行测试程序集,并检查输出。

  • 有关测试输出发送位置及其格式的详细信息,请参阅 NUnit 文档。
  • 根据我的经验,NUnit 编写了 XML 结果文件,我可以查看这些文件来查看测试结果。 XML 文件具有足够的可读性,可以使用文本编辑器查看并理解。

第 5 部分:迭代直至有效;调整测试代码并重复,直到成功。

  • 有时事情第一次就完美无缺,但往往并非如此。
  • 如果您将 Visual Studio 构建设置为将更新的、重建的测试程序集部署到测试运行器区域,则只需手动触发“计划”任务即可。

就我个人而言,我更喜欢 Windows 任务计划程序方法而不是 runas 方法。
对我来说,一旦设置完毕,迭代测试代码并重试测试就会更快、更容易。

正如我所提到的,NUnit3 有一个新的“NUnit-Lite”运行程序,可以轻松地将 NUnit 和您的测试打包到单个 EXE 控制台应用程序中。有关执行此操作的文档位于: https://docs.nunit .org/articles/nunit/running-tests/NUnitLite-Runner.html

总的来说,我更喜欢 NUnit-Lite 单一 EXE 方法,而不是 NUnit-Console 方法,并与 Windows 任务计划程序结合使用。

精通 PowerShell 和批处理文件的人可能更喜欢 RunAs 方法。

I've consolidated the key points into one answer and provided an outline of the steps involved.

I've also added information on using new NUnit 3 "NUnit-Lite" single-exe tests+runner, and using the Windows Task Scheduler as a simple mechanism to run the test(s) under the user account.

Darin Dimitrov's answer is on the money overall, but a couple of excellent points are buried in comments, and some people might want more guidance on how to set this up. Plus NUnit-Lite was not available when he wrote his answer.

Part 1: Place the test(s) that need to be run under the specific user account into their own separate assembly. Approximate steps:

  • For example, in Visual Studio you would select File/New Project, and select the NUnit project template (in Framework or .NET Core, as appropriate), name the project appropriately.

  • Move the code for the specific tests involved into the new assembly you just created. Get the assembly to build and run, and to give expected results with the account you are logged in under in Visual Studio. Don't worry about other user accounts just yet.

  • Option: Use the NUnit-Lite single EXE test runner packaging approach, new in NUnit3, as documented here: https://docs.nunit.org/articles/nunit/running-tests/NUnitLite-Runner.html

Part 2: Deploy the test assembly to the location where you will be running it, in order to be able to use the nunit-console.exe program to run the tests in the assembly.

  • See NUnit documentation for runtime requirements.

Part 3: Set up to execute nunit-console or your NUnit-Lite single EXE under the specific user account.

  • Option 1: use "runas" approach
    runas.exe /user:DOMAIN\someUser "nunit-console.exe somelibrary.dll"
    OR
    runas.exe /user:DOMAIN\someUser "MyTestsAndNUnitLiteRunner.exe"

  • I believe you would start up a Command Prompt; in Windows 10 it would be Start/(type CMD) and then you would right-click on the "Command Prompt" choice, and select "Run as different user", and then enter the userID and credentials for the specific user.

  • In some versions of Windows 10, you won't see the "Run as different user" option. If that happens, refer to this article: https://superuser.com/questions/1045158/how-do-you-run-as-a-different-user-from-the-start-menu-in-windows-10

  • Option 2: use Windows Task scheduler to set up a job that runs under the desired user account. You will need the user account login and credentials during the task setup process. Once the scheduled task is set up, you can just right-click on it and select "run" to run it immediately. The command line will be similar to this:
    "nunit-console.exe" somelibrary.dll
    OR, for NUnit-Lite:
    "MyTestsAndNUnitLiteRunner.exe"

In the task setup, you many need to use folder names and to use quotes if the paths involved have spaces in them.

Part 4: Run the test assembly, using your chosen approach, and review the output.

  • See NUnit documentation for the specifics of where test output gets sent and its format.
  • In my experience, NUnit wrote XML result files that I could go look at to see the test results. The XML files are human-readable enough to view with a text editor and understand.

Part 5: Iterate until it works; adjust test code and repeat until success is achieved.

  • Sometimes things work perfectly the first time, but often not.
  • If you set up your Visual Studio build to deploy the updated, rebuilt test assembly to your test runner area, you can just trigger the "scheduled" task manually.

Personally, I prefer the Windows Task Scheduler approach over the runas approach.
Once it's set up, to me, it's faster and easier to iterate on the test code and retry the test.

As I mentioned, NUnit3 has a new "NUnit-Lite" runner that makes it simple to package up NUnit AND your tests into a single EXE console app. Docs on doing that are here: https://docs.nunit.org/articles/nunit/running-tests/NUnitLite-Runner.html

Overall the NUnit-Lite single EXE approach would be my preference, over the NUnit-Console approach, in conjunction with Windows Task Scheduler.

People who are super-handy with PowerShell and batch files may prefer the RunAs approach.

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