Home > Mobile >  OneTimeTearDown not executing C# UI Testing
OneTimeTearDown not executing C# UI Testing

Time:06-17

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);    
            }
        }
    }

CodePudding user response:

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' to i >= 0`

CodePudding user response:

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($"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);
        }
    }
}

CodePudding user response:

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.

  • Related