OneTimetEardown不执行C#UI测试

发布于 2025-02-07 16:51:39 字数 1938 浏览 2 评论 0原文

我很难被拆除以执行的执行,该执行应该删除我的自动化在应用程序上创建的所有帐户。我选择使用OneTimetEardown,因此它执行一次,并且仅通过列表,但似乎并没有执行。这是代码,我把它放在错误的地方吗?如果我只将其添加到测试的末尾,则拆卸中的代码按原本打算工作。

    namespace Ag.AutomatedRegression.UI.Tests.AccountsTests
    {
        [TestFixture]
        public class AccountsPageTests : BaseSetup
        {
            [OneTimeTearDown]
            public void CleanUpAccounts()
            {
    
                LogList.Add($"Entered the {MethodBase.GetCurrentMethod()}().");
                for (int i = AccountsPageTests.accountIdList.Count - 1; i >= 0; i--)
                {
                    if (AccountsPageTests.accountIdList.Count == 0)
                    {
                        LogList.Add("There were no ids in the list to delete.");
                        break;
                    }
                    else
                    {
                        LogList.Add("Next Step: Clean Up accounts created this session.");
                        PerformBrowserManagerAction.NavigateToURL(CommonSettings.Default.URL);
                        PerformCommonStagesAction.CleanUpAccounts(CommonSettings.Default.UserName,
                                                                  CommonSettings.Default.EncryptedPassword,
                                                                  AccountsPageTests.accountIdList[i]);
                    }
    
                    LogList.Add("Account Cleanup was successful.");
                }
            }
            public static List accountIdList = new List();
            
    
    
            [Test]
            public void CreateAccountTest()
            {
                LogList.Add($"Entered the {MethodBase.GetCurrentMethod()}().");
    
                //Code to create account with account id string variable
               
                Assert.That(Success, Is.True, expectedMessage + "\r\n" + 
        actualMessage);    
            }
        }
    }

I am having trouble getting a teardown to execute that is supposed to delete all of the accounts my automation creates on our application. I elected to use a OneTimeTearDown so it executes once and just goes through the list but it doesn't seem to be executing. Here is the code, am I putting it in the wrong place maybe? The code in the teardown works as intended on its own if I just add it to the end of the test.


    namespace Ag.AutomatedRegression.UI.Tests.AccountsTests
    {
        [TestFixture]
        public class AccountsPageTests : BaseSetup
        {
            [OneTimeTearDown]
            public void CleanUpAccounts()
            {
    
                LogList.Add($"Entered the {MethodBase.GetCurrentMethod()}().");
                for (int i = AccountsPageTests.accountIdList.Count - 1; i >= 0; i--)
                {
                    if (AccountsPageTests.accountIdList.Count == 0)
                    {
                        LogList.Add("There were no ids in the list to delete.");
                        break;
                    }
                    else
                    {
                        LogList.Add("Next Step: Clean Up accounts created this session.");
                        PerformBrowserManagerAction.NavigateToURL(CommonSettings.Default.URL);
                        PerformCommonStagesAction.CleanUpAccounts(CommonSettings.Default.UserName,
                                                                  CommonSettings.Default.EncryptedPassword,
                                                                  AccountsPageTests.accountIdList[i]);
                    }
    
                    LogList.Add("Account Cleanup was successful.");
                }
            }
            public static List accountIdList = new List();
            
    
    
            [Test]
            public void CreateAccountTest()
            {
                LogList.Add($"Entered the {MethodBase.GetCurrentMethod()}().");
    
                //Code to create account with account id string variable
               
                Assert.That(Success, Is.True, expectedMessage + "\r\n" + 
        actualMessage);    
            }
        }
    }

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

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

发布评论

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

评论(3

凤舞天涯 2025-02-14 16:51:39

您的陈述索引

for (int i = AccountsPageTests.accountIdList.Count - 1; i > 0; i--)

并不能执行您想要的。它永远不会处理列表项目0,如果只有1个项目,则不会显示任何内容。

在心理上逐步逐步逐步逐步逐步逐步逐步完成

  • 假设一个项目计数-1 IS 0
  • 0不大于0
  • ,则

。 0'至 i> = 0`

The indexing on your statement

for (int i = AccountsPageTests.accountIdList.Count - 1; i > 0; i--)

doesn't do what you want. It will never process list item 0 and if there is only 1 item, nothing will show.

Step through it mentally, assuming one item

  • Count - 1 is 0
  • 0 is not greater than 0
  • so no items are executed

Change i > 0' toi >= 0`

温折酒 2025-02-14 16:51:39

正如我们已经在评论中讨论的那样,请尝试是否格式化是否是您班级的问题:

using NUnit.Framework;

namespace Your.Tests
{
    [TestFixture]
    public class AccountsPageTests : BaseSetup
    {
        private List<string> accountIdList = new List<string>();

        [OneTimeTearDown]
        public void CleanUpAccounts()
        {
            LogList.Add($"Entered the {MethodBase.GetCurrentMethod()}().");
            for (int i = AccountsPageTests.accountIdList.Count - 1; i >= 0; i--)
            {
                if (AccountsPageTests.accountIdList.Count == 0)
                {
                    LogList.Add("There were no ids in the list to delete.");
                    break;
                }
                else
                {
                    LogList.Add("Next Step: Clean Up accounts created this session.");
                    PerformBrowserManagerAction.NavigateToURL(CommonSettings.Default.StagesURL);
                    PerformCommonAction.CleanUpAccounts(CommonSettings.Default.StagesUserName,
                                                            CommonSettings.Default.StagesEncryptedPassword,
                                                            AccountsPageTests.accountIdList[i]);
                }

                LogList.Add("Account Cleanup was successful.");
            }
        }

        [Test]
        public void CreateAccountTest()
        {
            LogList.Add($"Entered the {MethodBase.GetCurrentMethod()}().");
            //Code to Create account with accountId as a string 
            accountIdList.Add(accountId);
        }
    }
}

As we were already discussing in the comments, please try whether formatting is an issue of your class:

using NUnit.Framework;

namespace Your.Tests
{
    [TestFixture]
    public class AccountsPageTests : BaseSetup
    {
        private List<string> accountIdList = new List<string>();

        [OneTimeTearDown]
        public void CleanUpAccounts()
        {
            LogList.Add(
quot;Entered the {MethodBase.GetCurrentMethod()}().");
            for (int i = AccountsPageTests.accountIdList.Count - 1; i >= 0; i--)
            {
                if (AccountsPageTests.accountIdList.Count == 0)
                {
                    LogList.Add("There were no ids in the list to delete.");
                    break;
                }
                else
                {
                    LogList.Add("Next Step: Clean Up accounts created this session.");
                    PerformBrowserManagerAction.NavigateToURL(CommonSettings.Default.StagesURL);
                    PerformCommonAction.CleanUpAccounts(CommonSettings.Default.StagesUserName,
                                                            CommonSettings.Default.StagesEncryptedPassword,
                                                            AccountsPageTests.accountIdList[i]);
                }

                LogList.Add("Account Cleanup was successful.");
            }
        }

        [Test]
        public void CreateAccountTest()
        {
            LogList.Add(
quot;Entered the {MethodBase.GetCurrentMethod()}().");
            //Code to Create account with accountId as a string 
            accountIdList.Add(accountId);
        }
    }
}
巨坚强 2025-02-14 16:51:39

在MU88的回答之后,格式是正确的,但这并不能解决拆卸,我最终使其成为标准的拆卸,而这似乎已经解决了问题。

Formatting was correct after mu88's answer, but that didn't fix the teardown, I ended up making it a standard Teardown instead and that seems to have resolved the issue.

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