How To Use Asserts In NUnit Using Selenium?
Himanshu Sheth
Posted On: February 8, 2021
64664 Views
21 Min Read
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium NUnit Tutorial.
When you are working on unit tests, there are scenarios where you wished that the execution should have stopped but alas, the most important part, ‘The Assertion,’ was missed. While developing tests using different test frameworks, my fellow developers frequently asked one question: ‘Should I use an assert or an exception in my test code’? It depends on what the test code should do after a certain condition is met.
By the end of this article, you will get in-depth insights about NUnit asserts and how these asserts can be used with C# & Selenium. I will walk you through the Assertions’ basics and when assertions should be used to get started. We will be covering the following constraints (discussed later) used with NUnit assert:
- Equal Constraints
- Comparison Constraints
- String Constraints
- Condition Constraints
- Collection Constraints
TABLE OF CONTENT
If you already know the constraints, you can head straight to the sections (NUnit Asserts – In Action and NUnit Asserts – Cross Browser Testing) that demonstrate usage of NUnit assert in the context of test automation.
What are Assertions?
In very simple terms, Assertions should be used to check something (a condition) that should never occur, whereas an exception should be used to check something (a condition) that might occur. For example, when a division operation is used, an exception can be raised if you get the ‘Divide by 0’ error. On the other hand, when using Selenium automation testing with C# (or another programming language), an assertion can be raised if the Page Title does not match the expected Title.
Below is a detailed list that highlights the use of assertions:
- Provide feedback about the source code to the development team.
- Check whether the implementation is correct, else there could be a severe issue with the code.
- Check preconditions and postconditions that can occur in the code.
What if you don’t assert at the right place? Well, it is like the situation shown below – walking on stairs where steps are broken, you are bound to get hurt ☺.
The NUnit Framework
The NUnit framework is a widely used open-source test framework used with C# and Selenium. It is based on the JUnit framework. The latest version is NUnit3.
When using C# for development & testing, I tried other test frameworks, MSTest/Visual Studio, xUnit.NET, etc., the advantages of NUnit over-power the different frameworks. Annotations in NUnit help speed the development and execution of the tests with a wide range of inputs.
If you plan to use TDD (Test Driven Development) for testing, you should try out the NUnit framework. In case you are looking for in-depth coverage on the NUnit framework with Selenium along with steps for installation on Visual Studio, you should check out the NUnit tutorial on setting up NUnit environment with Visual Studio in more detail.
Arrange, Act, and Assert
The AAA (Arrange, Act, and Assert) is a common practice for writing unit tests.
- Arrange – This section describes the setup required to execute the test.
- Act – This section executes the unit being tested, with the output being a store for evaluation.
- Assert – This section verifies whether the tests behaved as per expectations.
Many developers use ‘Given,’ ‘When,’ and ‘Then’ instead of AAA as it gives more descriptive meaning to these phrases, and many BDD tools also use these phrases to name their testing keywords.
The advantage of the AAA approach is that tests become more modular, thereby improving the tests’ readability.
Assertions in NUnit
NUnit has a rich set of assertions, which are static methods of the Assert Class.
1 |
public class Assert |
However, many developers have multiple asserts in one test, but that is not a good programming practice since the tests following the first NUnit assert do not get executed. Hence, it is recommended to have only one NUnit assert per test.
Before NUnit 2.4, Classic Model was used for using NUnit asserts. In this model, each NUnit assert was used via a separate method of the Assert Class. From NUnit 2.4 onwards, Constraint Model was introduced where a single Assert Class method was used. Constraint Object is passed as an argument with the details of the test that needs to be performed.
Take this certification to master the fundamentals of Selenium automation testing with C# and prove your credibility as a tester.
Here’s a short glimpse of the Selenium C# 101 certification from LambdaTest:
Helper Classes
Helper classes in C# are classes that provide methods that ‘assist/help’ get something done. Below are the helper classes to provide constraints to the assert method.
- Is
- Has
- Throws
- Contains
- Does
1 |
Assert.That( myString, Is.EqualTo("LambdaTest") ); |
In the example that was shown above, Is is the helper class, and EqualTo is the constraint.
Constraints in NUnit assert
There are eight broad categories of constraints:
1. Equal Constraints
This category of constraint is used to compare the expected result with the actual result. The Equal operation can be for equality (EqualTo) and non-equality (NotEqualTo)
a. EqualTo
It is used to check whether the actual result is the same as the expected result. If the results are not the same, assert is raised.
Syntax:
1 |
Is.EqualTo( object expected ) |
Here is a Selenium automation testing example that demonstrates the use of EqualTo constraint.
FileName – 1_Equal_To_Constraint.cs
In the above code, we have provided two parameters in the Assert.That method, which is an expected result and EqualTo constraint. The actual URL is compared with the Test URL (i.e., https://www.lambdatest.com/”).
1 |
Assert.That(actual_url, Is.EqualTo(test_url)); |
Assert error will be thrown if the values do not match, and the program execution will get terminated at this same line, i.e., the assertion statement itself.
b. NotEqualTo
As the name suggests, NoEqualTo does the opposite of EqualTo. It is used to check whether the actual result is not the same as the expected result. Assert will be raised if the results match.
Syntax:
1 |
Is.Not.EqualTo( object expected ) |
Here is a Selenium automation testing example that demonstrates the use of Not.EqualTo constraint. The implementation remains the same as the previous example, except that the check is now done for inequality.
FileName – 2_Equal_To_Constraint.cs
In the above code, we have provided two parameters in the Assert.That method, which is an expected result and Not.EqualTo constraint. Actual URL is compared with the Test URL (i.e., https://www.lambdatest.com”). Though the URLs are the same, the command driver.Url will return https://www.lambdatest.com/, which does not match the Test URL; hence, the test passes and assert is not raised.
2. Comparison Constraints
This category of constraint in NUnit assert is used to test whether one object is greater than the other. They are designed in a way that they can be read in mathematical order. The comparison operation can be for equality GreaterThan (or GreaterThanOrEqualTo), LessThan (or LessThanOrEqualTo), or checking if the result lies in a particular range (InRange).
Except for the InRange constraint, all the other comparison constraints can be used along with Not, i.e., NotGreaterThan/NotLessThan/etc.
a. GreaterThan
It is used to check whether the result is greater than the one supplied in the GreaterThan constraint. This constraint can be used with input values of data types like integer, float, double, long, etc.
Syntax:
1 |
Is.GreaterThan( object expected ) |
Example:
1 |
Assert.That(arg1, Is.Greater.Than(arg2)); |
To demonstrate the usage of the Nunit assert using Greater.Than constraint, we take a simplified example where Assert is raised if the actual value is greater than the value against which the test is performed.
FileName – 3_Greater_Than_Constraint.cs
As shown in the above example, assert is raised if the actual value is greater than 30. As you can see, the parameter inputted to the constraint is 45, which is greater than 30; hence, the test fails and assert is raised.
b. GreaterThanOrEqualTo
It is used to check whether the result is greater or equal to the one supplied in the GreaterThanEqualTo constraint.
Syntax:
1 |
Is.GreaterThanOrEqualTo( object expected ) |
Sample Usage:
1 |
Assert.That(arg1, Is.GreaterThanOrEqualTo(arg2)) |
It can also be used along with the Not constraint, as shown below.
1 |
Assert.That(arg1, Is.Not.GreaterThanOrEqualTo(arg2)) |
c. LessThan
It is used to check whether the result is less than the one supplied in the LessThan constraint. Assert will be raised if the input value is greater than the expected value. It can also be used along with Not where the intent of the operation is negated.
Syntax:
1 |
Is.LessThan( object expected ) |
Example:
FileName – 4_Less_Than_Constraint.cs
In the example shown above, assert will be raised if the actual value is not less than the LessThan constraint’s value. Since the input value (25) is less than the expected value (30), no assert is raised.
d. LessThanOrEqualTo
It is used to check whether the result is less or equal to the one supplied in the LessThan constraint. Assert will be raised if the input value is greater than the expected value.
Syntax:
1 |
Is.LessThanOrEqualTo( object expected ) |
Example:
1 2 |
Assert.That(arg1, Is.LessThanOrEqualTo(arg2)); Assert.That(arg1, Is.Not.LessThanOrEqualTo(arg2)); |
e. InRange
When using Selenium automation testing, there could be scenarios where you may want to test if a value lies within a particular range. The InRange constraint can be used to check in such a scenario.
It is a combination of two constraints – LessThanOrEqualTo and GreaterThanOrEqualTo.
Syntax:
1 |
Is.InRange( object from, object to ) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using NUnit.Framework; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assert_Demo { class Assert_Demo_1 { [Test] public void test_comparison() { Int32 actual_value = 25; // Raise an assert if the InRange is not successful Assert.That(actual_value, Is.InRange(25,30)); Console.WriteLine("Test for InRange Passed"); } } } |
As shown in the above example, the input value, i.e., 25, is within the range of 25~30. Hence, no assert is raised, and the test passes.
3. String Constraints
These set of constraints are used when NUnit assert has to be raised when examining string values. String values can be checked for equality (with or without ignoring case), whether a particular sub-string is present in the input string, whether a string is empty (or not), etc.
a. String Equal or Not Equal
This constraint is used to check whether the resultant string is the same (or not) as the string supplied in the Is.EqualTo or Is.Not.EqualTo constraint. The comparison is case-sensitive.
Syntax:
1 2 3 |
// Data type of the input value is string Is.EqualTo( object expected ) Is.Not.EqualTo( object expected ) |
Example:
FileName – 6_String_EqualTo_Constraint.cs
In the above example that demonstrates the usage of NUnit assert with Selenium automation testing, we open the URL under test on the Chrome browser. An instance of Chrome WebDriver is instantiated in the [SetUp] annotation, and actual business logic is placed in the [Test] annotation.
The site title (site_title) is extracted using the Web browser command driver.Title. A string-based comparison is made with the expected title. Assert is raised if the titles do not match. As seen in the output below, the string comparison passes. Hence, no assert is raised.
b. String Equal with IgnoreCase
This is a case-insensitive version of String Equal as the case is ignored using the IgnoreCase constraint.
Syntax:
1 |
IgnoreCase { get; } |
Example:
The logic in the previous example can be changed to ignore case-sensitivity. The change will be in the EqualTo constraint.
1 |
Assert.That(expected_title, Is.EqualTo(site_title).IgnoreCase); |
c. DoesContain
The DoesContain (and DoesNotContain) constraints are used to check if a sub-string is present in a given string. Assert is raised if the search is not successful.
It makes use of the Does Helper function, and the sub-string search can be case-insensitive.
Syntax:
1 |
IgnoreCase { get; } |
Example:
FileName – 7_SubString_Constraint.cs
In the above example, the sub-string to be searched is ‘lambdatest.’ The search for the sub-string is case-insensitive. If the sub-string is present in the site title (URL under test is https://www.lambdatest.com), no assert is raised, and the test passes.
1 2 |
// Raise an assert if the substring LambdaTest is not present Assert.That(site_title, Does.Contain(substring_title).IgnoreCase); |
As the sub-string (i.e., lambdatest) is present in the site title, no assert is raised.
d. Empty
This constraint is used to check if the resultant string is Empty or not. If Is.Empty is used, assert is raised if the result is not empty. If Is.Not.Empty is used, assert is raised if the result is empty i.e., Blank string.
Syntax:
1 |
Assert.That(arg1, Is.Empty); |
Example:
We modify the previous example that demonstrated the DoesContain constraint to check if the Site title for https://www.lambdatest.com is empty or not. Assert should be raised if the Site title is empty. Below is the code snippet since the majority of the implementation from the perspective of NUnit assert remains the same.
Empty_Constraint.cs
e. StartWith and EndWith
The StartWith and EndWith constraints are used with the IS helper functions to check if a resultant string starts with or ends with a particular string respectively. Assert is raised if the conditions for the search are not met. You can also make use of Not to negate the intent of the search.
Syntax:
1 2 |
Assert.That(str1, Does.StartWith("str2")); Assert.That(str1, Does.Not.StartWith("str2")); |
1 2 |
Assert.That(str1, Does.EndWith("str2")); Assert.That(str1, Does.Not.EndWith("str2")); |
Example:
For demonstrating the use of StartWith and EndWith constraints, we again make use of the example where the site title is checked whether assertion should be raised or not.
A case-insensitive check is done on the Site title, whether it starts with “cross” and whether it ends with “lambdatest”. If either of these operations are not successful, assert is raised.
1 2 3 4 5 |
Assert.That(site_title, Does.StartWith(start_with_str).IgnoreCase); Console.WriteLine("Site title " + site_title + " starts with " + start_with_str); Assert.That(site_title, Does.EndWith(end_with_str).IgnoreCase); Console.WriteLine("Site title " + site_title + " ends with " + end_with_str); |
The test passes since the strings are present at the respective positions i.e. Start and End.
f. Regex (Does.Match)
In case you plan to use regular expressions as a part of NUnit assert, you can make use of DoesMatch (or DoesNotMatch) constraint. It validates if the value matches a regular expression.
Syntax:
1 2 |
Assert.That(result, Does.Match("regex expression")); Assert.That(result, Does.Not.Match("regex expression")); |
Example:
For demonstration, we create a regular expression to match whether the title starts with “Cross.”
1 |
string pattern = @"\b[Cross]\w+"; |
A match against the regular expression, and assert raised if the Site title does not start with “Cross.”
1 |
Assert.That(site_title, Does.Match(pattern)); |
4. Condition Constraints
Condition constraints are used to check if a specific condition is satisfied or not. It could be a Boolean condition, Null, or Empty.
a. Empty
This constraint is used to check for Empty value. Assert is raised depending on whether you are checking if the field should be Empty or not.
Syntax
1 2 3 |
Assert.That(arg1, Is.Empty); Assert.That(arg1, Is.Not.Empty); |
Example
In the above Selenium automation testing demonstrating the usage of NUnit assert, the input URL is https://accounts.lambdatest.com/login. Using the Inspect tool, we locate the XPath of the email text box and fill the email text box with email-address (using SendKeys method).
We check whether the email-address text box is EMPTY or not? Assert is raised if the text box is not EMPTY.
1 2 3 4 5 6 7 8 9 |
String user_name_xpath = "//*[@id='app']/section/form/div/div/input[1]"; IWebElement user_name_element = webDriver.FindElement(By.XPath(user_name_xpath)); user_name_element.SendKeys("abc@gmail.com"); String user_name = user_name_element.GetAttribute("value"); Console.WriteLine("User Name" + user_name); Assert.That(user_name, Is.Empty); |
The above test fails since it was expecting the email-address text box to be empty.
b. Null
This constraint in NUnit assert is used to check if a particular object is NULL or not. The object could be array, string, etc.
Syntax
1 |
Is.Null(object expected); |
c. Boolean
As the name indicates, it can be used to check if a particular condition is satisfied or not (True/False).
Syntax
1 |
Assert.That(arg1, Is.True); |
5. Collection Constraints
The Collection Constraints are useful when you want to examine collections and contents inside it or for comparing two collections. Array is one example of a collection. Since this category of constraint would be more useful for testing the back-end business logic (databases, etc.), we won’t cover it in more detail in this NUnit tutorial.
For highlighting the syntax of different constraints, we will use an array of integers:
1 |
int[] arr = new int[] {6, 7, 8}; |
a. Greater than
It is used to check if the values in a collection (array in this case) are greater than a ‘comparison’ value. NUnit assert is raised if any one of the items is less than the comparison value.
Example:
1 |
Assert.That(arr, Is.All.GreaterThan(4)); |
b. Less Than
It is used to check if the values in a collection (array in this case) are lesser than a ‘comparison’ value. NUnit assert is raised if any one of the items is greater than the comparison value.
Example:
1 |
Assert.That(arr, Is.All.LessThan(20)); |
c. Not Null
As the name signifies, it is used to check if there are no NULL values in the collection. You can even use this when performing a NULL check on string arrays. Assert is raised if any one of the items in the collection is found to be NULL.
Example:
Assert.That(arr, Is.All.Not.Null);
d. Instance Of
It is used to check if the items in a collection belong to one particular data-type (Int32, String, Float, etc.).
Example:
1 |
Assert.That(arr, Is.All.InstanceOf<Int32>()); |
e. Exactly X Items
Exactly constraint should be used to check if a collection contains an ‘X’ number of items. Assert is raised if the collection does not contain exactly X items.
Example
1 2 |
// Assert is raised if arr does not contain 3 items Assert.That(arr, Has.Exactly(3).Items); |
f. Unique Items
It can be used to verify if all the items in a collection have a unique value. Assert is raised if any one of the items is not unique.
Example:
1 2 |
// Assert is raised if arr does not contain unique values Assert.That(arr, Is.Unique); |
g. Empty
Like the Empty condition constraint, this constraint also does the same job except that it acts on a collection.
Example:
1 2 |
Assert.That(arr, Is.Not.Empty); Assert.That(arr, Is.Empty); |
h. Contains
Contains can be instrumental when you want to perform a non-linear search for a particular element in a collection. Assert is raised if the item (i.e. value) is not present in the collection.
Example:
1 2 |
//Check if a particular element is present in the collection Assert.That(array, Contains.Item(6)); |
Apart from these constraints, there are many other constraints (Directory/File constraints, Type/Reference constraints, and Exceptions constraints) that may not be relevant in the context of cross browser testing or Selenium automation testing. Hence, those are not covered as a part of this article.
NUnit Asserts – In Action
To demonstrate the usages of NUnit assert in Selenium automation testing, we take an example of a Sample ToDo app on LambaTest. We make use of the NUnit framework along with Selenium, relevant asserts would be used for testing & verification.
Test Case
- Open the Sample ToDo App – https://lambdatest.github.io/sample-todo-app/
- Assert if the Driver title does not match the Expected title
- Select the first two check-box items
- Find textbox with id = sampletodotext and add a new item to the list
- Verify whether the item has been added else raise an assert
Implementation
Test_Case_Assertion.cs
Code Walkthrough
We get the details about the check-box items via their name, i.e. li1, li2, etc., using the Inspect tool of the Chrome browser.
We then locate the text-box element where the text for the new element has to be added and add a button which enables the addition of the new text to the list.
1 2 3 4 5 6 |
IWebElement textfield = driver.FindElement(By.Id("sampletodotext")); textfield.SendKeys(itemName); // Click on Add button IWebElement addButton = driver.FindElement(By.Id("addbutton")); addButton.Click(); |
Once the new element has been added, we check whether it is a part of the list by verifying the text box’s content, i.e., it should not be empty. We make use of NUnit assert with Boolean Condition Constraint to verify the same.
1 2 3 4 5 6 7 |
// Verified Added Item name IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span")); String getText = itemtext.Text; // Check if the newly added item is present or not using // Condition constraint (Boolean) Assert.That((itemName.Contains(getText)), Is.True); |
Shown below is the execution snapshot where we can see that the new item is added to the list.
NUnit Asserts – Cross Browser Testing
As a web developer, you have performed cross browser testing on a local setup available for pursuit. Well Done! You feel that your job is complete, and your product is good to go live.
What if the first customer experience itself gives you sleepless nights☹. It is true that no one can control customer’s choices. What if the customer uses a web browser/OS combination on which you have never performed testing? It is time to rethink your cross browser testing strategy by focusing on cloud-based cross browser testing rather than locally. There are many advantages; the most significant is acceleration in the test process since tests can be performed parallel. You can verify your product’s functionalities (especially those that are browser/OS/device specific) on different browser combinations, thereby improving the test coverage.
LambdaTest is a cloud based cross browser testing platform where you can perform testing on close to 3000+ browsers and OS combinations. Once you can create an account on LambdaTest, you should choose a billing plan as per your requirements. We were performing local Selenium automation testing, but now it has to be ported to LambdaTest’s remote Selenium Grid. Doing so does not require much effort as the changes are related to infrastructure.
Browser capabilities can be generated using the LambdaTest capabilities generator. For using the platform, you would need a user-name & access-key, and this can be accessed from the LambdaTest Profile Page. You are all set to perform web browser testing on the remote Selenium Grid.
We implement two test cases to demonstrate the usage of NUnit assert on the remote Selenium Grid.
Test Case – 1
- Navigate to the URL https://www.lambdatest.com
- Locate the Login button and click on the same
- Verify the Title of the page and raise assert if the Title is not matching the expected value
Test Case – 2
- Navigate to the URL https://accounts.lambdatest.com/login
- Enter the user-name and password by locating the respective elements
- Raise assert if either of them is empty
- Click the Login Button
- Once logged-in, verify the title of the page else raise assert
Implementation
LT_Test_Case_Assertion.cs
Code Walkthrough
The combination of user-name and access-token is used to login to the platform. These are passed to the remote URL on which the Selenium automation testing will be performed.
1 2 3 4 5 |
String username = "user-name"; String accesskey = "access-key"; ....................................................................... ....................................................................... driver = new RemoteWebDriver(new Uri("https://user-name:access-key@hub.lambdatest.com/wd/hub"), capabilities, TimeSpan.FromSeconds(600)); |
The browser capabilities are generated using the LambdaTest capabilities generator.
1 2 3 4 5 6 |
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.SetCapability("user", "user-name"); capabilities.SetCapability("accessKey", "access-key"); capabilities.SetCapability("build", "Assert Test using LambdaTest (C#)"); capabilities.SetCapability("name", "Assert Test using LambdaTest (C#)"); ....................................................................... |
Test Case 1 – We locate the login button using XPath locator. Once the element is located, a Click action is performed to navigate to the target page.
1 2 |
IWebElement login_button = driver.FindElement(By.XPath("//*[@id='navbarSupportedContent']/ul/li[6]/a")); login_button.Click(); |
Once the LambdaTest login page opens, the title of the page is compared with the expected title using EqualTo constraint. If they do not match, assert is raised.
1 2 3 |
String expected_title = "login - Lambdatest"; String actual_title = driver.Title; Assert.That(expected_title, Is.EqualTo(actual_title).IgnoreCase); |
Test Case 2 – Like Test case (1), required elements (user name & password text boxes and login button) are located on the LambdaTest login web page. Once those elements are located, the respective actions are performed on those elements, i.e., user-name and password are entered in the boxes, and the login button is clicked.
1 2 3 4 5 6 |
String user_name_xpath = "//*[@id='app']/section/form/div/div/input[1]"; .............................................................................. .............................................................................. IWebElement user_name_element = driver.FindElement(By.XPath(user_name_xpath)); user_name_element.SendKeys("validuser@gmail.com"); .............................................................................. |
Once it navigates to the target page, i.e., LambdaTest dashboard, the title is checked. If it does not match the expected title, assert is raised.
1 2 3 4 |
String expected_title = "Welcome - LambdaTest"; String actual_title = driver.Title; Assert.That(expected_title, Is.EqualTo(actual_title).IgnoreCase); |
We visit the AUTOMATION LOGS section to check the status of the test. As seen in the snapshot, the test status is Completed, i.e., the test has passed.
It’s a Wrap
In this brief NUnit tutorial with a focus on NUnit assert, we looked at different asserts and constraints helpful in Selenium automation testing. The intent of using assert in test code is to halt the execution as soon as assert is encountered. To improve test coverage and verify product functionalities across different browser & operating system combinations, developers can explore Selenium testing on the cloud. The advantage is that the core business logic remains intact, with minimal changes required to port the NUnit test code to the remote Selenium Grid.
Enhance your NUnit interview proficiency with our meticulously curated compilation of questions and answers. Explore the comprehensive list of NUnit Interview Questions and Answers for valuable insights.
Do leave the frequently used NUnit asserts in the comments section to learn from each other’s experience.
Happy Testing ☺
Got Questions? Drop them on LambdaTest Community. Visit now