TestNG Groups

Dmitry Shyshkin
A free video tutorial from Dmitry Shyshkin
I'll teach you automate tests.
4.6 instructor rating • 5 courses • 12,953 students

Learn more from the full course

Selenium WebDriver with Java for beginners

Selenium WebDriver with Java and TestNG. Tutorial designed for complete beginners in Selenium testing and automation

06:31:37 of on-demand video • Updated August 2020

  • Set up test automation framework with Selenium WebDriver
  • Automate your first WebDriver test
  • Learn basics of Java for testers
  • Execute Selenium tests on different browsers
  • Use TestNG features for better test structure and easy selenium framework maintenance
  • Recognize different selenium test failures and exceptions and know how to fix and avoid them
English [Auto] Hi, guys. In the last week, though, we learned how to use test injury annotation parameters. Today, we will continue working with annotations and parameters and learn how to use groups. Let's open documentation page and click on five to test groups. Group in tests are really useful and best, and G lets you add one or more groups to tests and then you can include or exclude specific groups during test execution. Here you can read more details about this. So let's copy this example, how to use groups and added to our tests in the negative test class. So let's copy this part inside of the brackets and let's paste what we cooperate here inside of the brackets. Next next to at test annotation after korma. So priority equals one comma. And let's paste groups and let's go down to our second test and do the same. So here after enabled comma and let's based groups and back up because we have just two tests. It's not a very useful example of using groups. It makes sense to use groups when we have lots of tasks. But just to learn how to do that, we will use this to test method in in our example. So since this is negative test class, are all tests in this test class will be negative test. So let's type negative tests as the first group and let's copy it and go down and. Based. Here to our second test and also let's create test group called Smoke Test. So we will add this group to tests that we want to include in our smoke test or sanity test. And let's just do it for a first test. So only incorrect username test will be included in smoke test group, but incorrect password will not. So let's remove the second part of the example and leave just one group for the second test. So now our both tests belong to a negative test group, but only incorrect user name tests belong to smoke test group. So let's save and let's open our negative test examples sweep file. So let's assume that we need to quickly run tests for a new release and we do not have time for all tests. So we want to run only test that belong to smoke test group. So let's switch to the documentation Web site and see the example how to use test group in the sweet smell file. As you see here in this example, this is how easy to include only specific group. So let's copy this and use it in the negative test X amount sweep file. So let's just copy this part. Groups around. Include run groups. Kopi. And here after past, let's face it. And as they like to do, Quantrell. Shift F to format to make our test look better. So now let's replace this group name from the example with our smoke test group name. So here, smoke tests, Copi. And let's go here and paste. So basically we say run a negative test class, but include only tests that are belong to a smoke test group, but also in the last test. We excluded. We disabled second test. So let's remove this. So both tests are enabled and separated by Coloma. Both tests are enabled, but one belongs to only negative tests and one belongs to negative. And smoke this. So now we saved and let's run our negative tests and make sure that only test that belongs to smoke test group is executed. Important to remember, Java is a case sensitive language. All were viable names must match the case. So if in your test, your group name is small tests was an uppercase T. You need to do exactly the same in the tests with otherwise. This group will not be executed. And this is not just for groups in tests. This is basically for everything you write in Java. Case matters. So test finished one and only incorrect user name test was a good executed and incorrect user name. Test is the one that belongs to smoke test group. Also not related to groups, but in the similar way we can include or exclude specific test methods. So first, let's comment our two group part. So it doesn't interfere with what we are doing. Now let's select our group's part and source the block comment to comment. Our groups. And now we need to change a little bit. This class PAC. So instead of wine line class, we will open and close it in the separate in the separate lanes. So class open the class clause. Let's save them here. Inside. Let's add next. So Matt Mathilde's. Controlled space now doesn't want to outlaw suggest so. So my thoughts close and inside include. Doesn't want to alter suggests salt include. We need to type name. I'm basically the same mosque here. Name we will put the name of that test match up and close our nametag and safe. Also inside of this, Mathilde's tech, there is another option to exclude test. Unfortunately, for some reason, the eclipse doesn't want to suggest. But we can include or exclude test methods. So, for example, when we have 10 mantels in the class and we want to run nine, it will make sense to exclude one. And when we have class with 10 matters. But we want to round just one. Then it's easier to include one method. So let's include. And let's go here. Let's let's this time include our second test method. So let's copy test my name. Go back here and paste our test method name here. Save. And let's run this test to make sure that incorrect password test will be executed. OK, so I made the Bible include time e l and I got an error, so let's fix, let's save and let's run again. And as you see, the test is saying, start an incorrect password, test the one we want, and let's repeat the same with exclude just for you guys to learn. So instead of include, I will put X Klute and I will exclude incorrect password, which means only incorrect username test should be executed, save and run. So test finished. And this time only incorrect username was executed because incorrect password was excluded. And of course when you are including or excluding test method name, you need to make sure that the test glass is the one that includes this test method. Otherwise, it makes no sense. So now you know a few different ways to include or exclude tests you can use enable parameter. You can group tests and around specific groups. Or you can include or exclude specific test methods in the test suite. ICSOM, I'll file.