Handling multiple assertions and fail test if none are found












1















I'm making a negative test case that tests if an error message pops up and then check if the message inside the modal dialog is correct. The problem is, the app has two languages: english and german so the error message can be in either of those two languages. What I have so far:



productsPage.CreateProduct("Test product-" + "2bd43cfd", "2bd43cfd", ".TEST Product-Buyer", "CC", "HRK", 40.94
string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
try
{
Assert.AreEqual("A product with identical service id and service provider already exists!", modalMessage);
test.Log(Status.Pass, "Error message '" + modalMessage + "' was found inside modal dialog.");
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
try
{
Assert.AreEqual("Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!", modalMessage);
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
}
catch (Exception e)
{
test.Log(Status.Fail, "Test execution failed: " + e.ToString());
throw;
}


As you can see, I want to use two assertions because the message can be in either english or german but the problem is, I want to fail the test if none of those two asserts pass.



How should I do this?










share|improve this question

























  • To test a single unit of functionality you should be able to control (mock) the elements that impact it. Therefore I would look to find a way to set the language specifically to English, have a test for that, and then to German and have a test for that.

    – sr28
    Nov 23 '18 at 15:28


















1















I'm making a negative test case that tests if an error message pops up and then check if the message inside the modal dialog is correct. The problem is, the app has two languages: english and german so the error message can be in either of those two languages. What I have so far:



productsPage.CreateProduct("Test product-" + "2bd43cfd", "2bd43cfd", ".TEST Product-Buyer", "CC", "HRK", 40.94
string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
try
{
Assert.AreEqual("A product with identical service id and service provider already exists!", modalMessage);
test.Log(Status.Pass, "Error message '" + modalMessage + "' was found inside modal dialog.");
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
try
{
Assert.AreEqual("Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!", modalMessage);
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
}
catch (Exception e)
{
test.Log(Status.Fail, "Test execution failed: " + e.ToString());
throw;
}


As you can see, I want to use two assertions because the message can be in either english or german but the problem is, I want to fail the test if none of those two asserts pass.



How should I do this?










share|improve this question

























  • To test a single unit of functionality you should be able to control (mock) the elements that impact it. Therefore I would look to find a way to set the language specifically to English, have a test for that, and then to German and have a test for that.

    – sr28
    Nov 23 '18 at 15:28
















1












1








1








I'm making a negative test case that tests if an error message pops up and then check if the message inside the modal dialog is correct. The problem is, the app has two languages: english and german so the error message can be in either of those two languages. What I have so far:



productsPage.CreateProduct("Test product-" + "2bd43cfd", "2bd43cfd", ".TEST Product-Buyer", "CC", "HRK", 40.94
string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
try
{
Assert.AreEqual("A product with identical service id and service provider already exists!", modalMessage);
test.Log(Status.Pass, "Error message '" + modalMessage + "' was found inside modal dialog.");
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
try
{
Assert.AreEqual("Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!", modalMessage);
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
}
catch (Exception e)
{
test.Log(Status.Fail, "Test execution failed: " + e.ToString());
throw;
}


As you can see, I want to use two assertions because the message can be in either english or german but the problem is, I want to fail the test if none of those two asserts pass.



How should I do this?










share|improve this question
















I'm making a negative test case that tests if an error message pops up and then check if the message inside the modal dialog is correct. The problem is, the app has two languages: english and german so the error message can be in either of those two languages. What I have so far:



productsPage.CreateProduct("Test product-" + "2bd43cfd", "2bd43cfd", ".TEST Product-Buyer", "CC", "HRK", 40.94
string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
try
{
Assert.AreEqual("A product with identical service id and service provider already exists!", modalMessage);
test.Log(Status.Pass, "Error message '" + modalMessage + "' was found inside modal dialog.");
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
try
{
Assert.AreEqual("Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!", modalMessage);
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
}
catch (Exception e)
{
test.Log(Status.Fail, "Test execution failed: " + e.ToString());
throw;
}


As you can see, I want to use two assertions because the message can be in either english or german but the problem is, I want to fail the test if none of those two asserts pass.



How should I do this?







c# selenium selenium-webdriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:48









Brian

4,58672640




4,58672640










asked Nov 23 '18 at 14:20









Bernard PolmanBernard Polman

11819




11819













  • To test a single unit of functionality you should be able to control (mock) the elements that impact it. Therefore I would look to find a way to set the language specifically to English, have a test for that, and then to German and have a test for that.

    – sr28
    Nov 23 '18 at 15:28





















  • To test a single unit of functionality you should be able to control (mock) the elements that impact it. Therefore I would look to find a way to set the language specifically to English, have a test for that, and then to German and have a test for that.

    – sr28
    Nov 23 '18 at 15:28



















To test a single unit of functionality you should be able to control (mock) the elements that impact it. Therefore I would look to find a way to set the language specifically to English, have a test for that, and then to German and have a test for that.

– sr28
Nov 23 '18 at 15:28







To test a single unit of functionality you should be able to control (mock) the elements that impact it. Therefore I would look to find a way to set the language specifically to English, have a test for that, and then to German and have a test for that.

– sr28
Nov 23 '18 at 15:28














2 Answers
2






active

oldest

votes


















2














The rule of thumb is to always know what do you check in a particular test case. If the test does not clearly know which message to verify (English or German) at the particular moment you need to keep in mind that the test will be 'green' if:




  1. You have an English mode on your app but the alert text is in German. (sounds like a bug. right?)

  2. You have a German mode but the alert is in English. (also does not sound correct)


I can think of 2 solutions:





  1. Create 2 tests and explicitly pass the "expected language":



    @Test
    public void EnglishTest() {
    //implement logic that will start your app in English mode
    startAppInEnglishMode();
    String expectedMessage = "A product with identical service id and service provider already exists!";
    checkAlert(expectedMessage);
    }

    @Test
    public void GermanTest() {
    //implement logic that will start your app in German mode
    startAppInGermanMode();
    String expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
    checkAlert(expectedMessage);
    }

    public void CheckAlert(string expectedMessage) {
    string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
    try {
    Assert.AreEqual(expectedMessage, modalMessage);
    } catch (NUnit.Framework.AssertionException) {
    test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
    }
    }



  2. Clearly define what mode is on before calling an assertion



    @Test
    public void singleTestForBothLanguages() {
    boolean isEnglishMode = isEnglishMode();
    string expectedMessage;
    if (isEnglishMode) {
    expectedMessage = "A product with identical service id and service provider already exists!";
    } else {
    expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
    }
    checkAlert(expectedMessage);
    }

    //for example check attribute of some element (maybe a flag? or whatever makes you sure to know which mode is on now)
    public bool isEnglishMode() {
    if ( ....){
    return true;
    }
    return false;
    }

    public void CheckAlert(string expectedMessage) {
    string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
    try {
    Assert.AreEqual(expectedMessage, modalMessage);
    } catch (NUnit.Framework.AssertionException) {
    test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
    }
    }







share|improve this answer































    1














    You can make use of Assert.True method instead.



    Your code will be like:



    Assert.True(modalMessage == "A product with identical service id and service provider already exists!" || 
    modalMessage == "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!");


    This way you'll check them both at the same time.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448389%2fhandling-multiple-assertions-and-fail-test-if-none-are-found%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      The rule of thumb is to always know what do you check in a particular test case. If the test does not clearly know which message to verify (English or German) at the particular moment you need to keep in mind that the test will be 'green' if:




      1. You have an English mode on your app but the alert text is in German. (sounds like a bug. right?)

      2. You have a German mode but the alert is in English. (also does not sound correct)


      I can think of 2 solutions:





      1. Create 2 tests and explicitly pass the "expected language":



        @Test
        public void EnglishTest() {
        //implement logic that will start your app in English mode
        startAppInEnglishMode();
        String expectedMessage = "A product with identical service id and service provider already exists!";
        checkAlert(expectedMessage);
        }

        @Test
        public void GermanTest() {
        //implement logic that will start your app in German mode
        startAppInGermanMode();
        String expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
        checkAlert(expectedMessage);
        }

        public void CheckAlert(string expectedMessage) {
        string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
        try {
        Assert.AreEqual(expectedMessage, modalMessage);
        } catch (NUnit.Framework.AssertionException) {
        test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
        }
        }



      2. Clearly define what mode is on before calling an assertion



        @Test
        public void singleTestForBothLanguages() {
        boolean isEnglishMode = isEnglishMode();
        string expectedMessage;
        if (isEnglishMode) {
        expectedMessage = "A product with identical service id and service provider already exists!";
        } else {
        expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
        }
        checkAlert(expectedMessage);
        }

        //for example check attribute of some element (maybe a flag? or whatever makes you sure to know which mode is on now)
        public bool isEnglishMode() {
        if ( ....){
        return true;
        }
        return false;
        }

        public void CheckAlert(string expectedMessage) {
        string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
        try {
        Assert.AreEqual(expectedMessage, modalMessage);
        } catch (NUnit.Framework.AssertionException) {
        test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
        }
        }







      share|improve this answer




























        2














        The rule of thumb is to always know what do you check in a particular test case. If the test does not clearly know which message to verify (English or German) at the particular moment you need to keep in mind that the test will be 'green' if:




        1. You have an English mode on your app but the alert text is in German. (sounds like a bug. right?)

        2. You have a German mode but the alert is in English. (also does not sound correct)


        I can think of 2 solutions:





        1. Create 2 tests and explicitly pass the "expected language":



          @Test
          public void EnglishTest() {
          //implement logic that will start your app in English mode
          startAppInEnglishMode();
          String expectedMessage = "A product with identical service id and service provider already exists!";
          checkAlert(expectedMessage);
          }

          @Test
          public void GermanTest() {
          //implement logic that will start your app in German mode
          startAppInGermanMode();
          String expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
          checkAlert(expectedMessage);
          }

          public void CheckAlert(string expectedMessage) {
          string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
          try {
          Assert.AreEqual(expectedMessage, modalMessage);
          } catch (NUnit.Framework.AssertionException) {
          test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
          }
          }



        2. Clearly define what mode is on before calling an assertion



          @Test
          public void singleTestForBothLanguages() {
          boolean isEnglishMode = isEnglishMode();
          string expectedMessage;
          if (isEnglishMode) {
          expectedMessage = "A product with identical service id and service provider already exists!";
          } else {
          expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
          }
          checkAlert(expectedMessage);
          }

          //for example check attribute of some element (maybe a flag? or whatever makes you sure to know which mode is on now)
          public bool isEnglishMode() {
          if ( ....){
          return true;
          }
          return false;
          }

          public void CheckAlert(string expectedMessage) {
          string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
          try {
          Assert.AreEqual(expectedMessage, modalMessage);
          } catch (NUnit.Framework.AssertionException) {
          test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
          }
          }







        share|improve this answer


























          2












          2








          2







          The rule of thumb is to always know what do you check in a particular test case. If the test does not clearly know which message to verify (English or German) at the particular moment you need to keep in mind that the test will be 'green' if:




          1. You have an English mode on your app but the alert text is in German. (sounds like a bug. right?)

          2. You have a German mode but the alert is in English. (also does not sound correct)


          I can think of 2 solutions:





          1. Create 2 tests and explicitly pass the "expected language":



            @Test
            public void EnglishTest() {
            //implement logic that will start your app in English mode
            startAppInEnglishMode();
            String expectedMessage = "A product with identical service id and service provider already exists!";
            checkAlert(expectedMessage);
            }

            @Test
            public void GermanTest() {
            //implement logic that will start your app in German mode
            startAppInGermanMode();
            String expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
            checkAlert(expectedMessage);
            }

            public void CheckAlert(string expectedMessage) {
            string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
            try {
            Assert.AreEqual(expectedMessage, modalMessage);
            } catch (NUnit.Framework.AssertionException) {
            test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
            }
            }



          2. Clearly define what mode is on before calling an assertion



            @Test
            public void singleTestForBothLanguages() {
            boolean isEnglishMode = isEnglishMode();
            string expectedMessage;
            if (isEnglishMode) {
            expectedMessage = "A product with identical service id and service provider already exists!";
            } else {
            expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
            }
            checkAlert(expectedMessage);
            }

            //for example check attribute of some element (maybe a flag? or whatever makes you sure to know which mode is on now)
            public bool isEnglishMode() {
            if ( ....){
            return true;
            }
            return false;
            }

            public void CheckAlert(string expectedMessage) {
            string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
            try {
            Assert.AreEqual(expectedMessage, modalMessage);
            } catch (NUnit.Framework.AssertionException) {
            test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
            }
            }







          share|improve this answer













          The rule of thumb is to always know what do you check in a particular test case. If the test does not clearly know which message to verify (English or German) at the particular moment you need to keep in mind that the test will be 'green' if:




          1. You have an English mode on your app but the alert text is in German. (sounds like a bug. right?)

          2. You have a German mode but the alert is in English. (also does not sound correct)


          I can think of 2 solutions:





          1. Create 2 tests and explicitly pass the "expected language":



            @Test
            public void EnglishTest() {
            //implement logic that will start your app in English mode
            startAppInEnglishMode();
            String expectedMessage = "A product with identical service id and service provider already exists!";
            checkAlert(expectedMessage);
            }

            @Test
            public void GermanTest() {
            //implement logic that will start your app in German mode
            startAppInGermanMode();
            String expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
            checkAlert(expectedMessage);
            }

            public void CheckAlert(string expectedMessage) {
            string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
            try {
            Assert.AreEqual(expectedMessage, modalMessage);
            } catch (NUnit.Framework.AssertionException) {
            test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
            }
            }



          2. Clearly define what mode is on before calling an assertion



            @Test
            public void singleTestForBothLanguages() {
            boolean isEnglishMode = isEnglishMode();
            string expectedMessage;
            if (isEnglishMode) {
            expectedMessage = "A product with identical service id and service provider already exists!";
            } else {
            expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
            }
            checkAlert(expectedMessage);
            }

            //for example check attribute of some element (maybe a flag? or whatever makes you sure to know which mode is on now)
            public bool isEnglishMode() {
            if ( ....){
            return true;
            }
            return false;
            }

            public void CheckAlert(string expectedMessage) {
            string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
            try {
            Assert.AreEqual(expectedMessage, modalMessage);
            } catch (NUnit.Framework.AssertionException) {
            test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
            }
            }








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 6:39









          Vladimir EfimovVladimir Efimov

          695311




          695311

























              1














              You can make use of Assert.True method instead.



              Your code will be like:



              Assert.True(modalMessage == "A product with identical service id and service provider already exists!" || 
              modalMessage == "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!");


              This way you'll check them both at the same time.






              share|improve this answer




























                1














                You can make use of Assert.True method instead.



                Your code will be like:



                Assert.True(modalMessage == "A product with identical service id and service provider already exists!" || 
                modalMessage == "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!");


                This way you'll check them both at the same time.






                share|improve this answer


























                  1












                  1








                  1







                  You can make use of Assert.True method instead.



                  Your code will be like:



                  Assert.True(modalMessage == "A product with identical service id and service provider already exists!" || 
                  modalMessage == "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!");


                  This way you'll check them both at the same time.






                  share|improve this answer













                  You can make use of Assert.True method instead.



                  Your code will be like:



                  Assert.True(modalMessage == "A product with identical service id and service provider already exists!" || 
                  modalMessage == "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!");


                  This way you'll check them both at the same time.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 14:30









                  Paul KaramPaul Karam

                  2,43261734




                  2,43261734






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448389%2fhandling-multiple-assertions-and-fail-test-if-none-are-found%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Berounka

                      Sphinx de Gizeh

                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...