Database Testing with Specflow
A free video tutorial from Karthik KK
Software Architect, Consultant,Youtuber and Best Seller
4.3 instructor rating • 29 courses • 252,074 students
In this lecture we will discuss about database testing with Specflow and how to achieve this using our DatabaseHelperExtension.
Learn more from the full courseAutomation framework development with Selenium C# (Advanced)
An End-to-End Selenium automation framework development course
15:52:08 of on-demand video • Updated January 2021
- Understand what an automation framework is
- Choosing right automation framework for your project
- Building more robust and hybrid automation framework
- Leveraging the power of C# to build generic framework model
- Extending the automation framework with Behavioral Driven Development with Specflow
- Developing an Industry standard framework with complete modularity on framework as well as Application test code
English [Auto] Database testing in shot database testing sounds very interesting, right? Well, but wait, we're not going to do a lot. Rather, we're going to talk, inspect flow and scenario prospective, which we have created so far in our previous videos. Which means we are going to talk a little extra, but not a lot deeper into the database testing part. We're going to make use of our database, help our extension class, which we created while working with our helpers, libraries in our course before. So let's see this in action so far that I'm going to flip to this studio. So this is the same project which we have been working so long and for Database Helper, we have already done the database exchange and our cars and here is the code which tries to connect to the database, execute some of the greatest and perform a close operation as well. So these are the methods which we already written while working with our previous section of this course. So what else we going to do? Basically, if you see our application every time, what happened is if we try to create a employee by clicking the create new and rendering all the details and the create button, it will create an employee for us. As you can see in our previous scenario, we created this user. So every time while we run our test, it automatically creates this other user and just kind of nagging, because if you keep on creating this of user, your test is going to run out maybe in your test or your application. There will be a condition where same name of the user cannot be created. So how to handle the situation? Well, in manual testing, what we basically do is we try to delete the existing user as they are the user, and then we try to create the same user and we see if the functionality is working fine or not. But for deleting, we either have to click delete button or the best way is instead of again opening the UI and clicking the delete button. But of course, for checking the delete functionality, this is fine. You click the direct link, but every time, just to see if the create employee is working fine or not, you cannot go and delete this user. So how to handle the situation? Well, the best, easiest way to handle the situation is delete the user from the database. So that's what we're going to do in here if I go to my Esquibel server and if I go to my database, let's try to query this database, select star from employees. And you can see that we have this ARTA user out of user two times here. So if I did it, this user, then my user is not going to be here. That's exactly what I'm going to do using our database help extension. I'm going to delete the existing user. So every time I'm going to run my create user scenario that we have in here create comply with all the details. So I'm going to call my background. So the background, remember, which will come under one when is starting to create the scenarios. I'm going to call this guy. So it says either the employee or the user before I start running the test. So here I'm going to create a user. And next time, if I'm going to run the same test, then I will delete the user so that my test runs without any problem. Even if an application have a condition that the same user name user cannot be created. This is still acceptable because you are deleting the user before you can run it. So that's what I'm going to do in this database testing. So before starting to implement this stuff, first of all, we need to make some changes to our framework. I'm going to go to my config and there is a conflict of interest. So here I'm going to add some code. So before adding code in the configurator, I also need to change the setting. Dorothea's so I have already written the code. I'm just going to pasted here and this is nothing but the application connection property. So basically it is going to connect to my application database and it is going to handshake with it and it's going to retrieve all the data for me. So I'm going to create a application connection object and also I need to have a application connecting the string. So this connection string is basically going to come from a config file. So in our framework, all the configurations are basically sitting in the global config XML file and here we don't have any time to address the application configuration. So I'm going to add a tag for our application database configuration. So the tag I'm going to add is application DB and this is the connection string for my Ezekial server. And this is the server name server and the initial catalog is going to be my employee database, so that's what I have mentioned here. I'm going to say it. And since I have added a tag here, I need to read the back from the configurator Nazis. So I need to add some of the export items. So the export item is going to be this. And I also need to get the item from the example. I need to read the attack. So the framework annunciating of application DB. So this is the tag and then I need to store that in my settings property. So the settings property is going to be like this. Apkarian string is equal to app connection, dot value, dot to string. So now or configurator is ready or setting starts. This is already in our global configuration. XML is ready and now we have read the connection, the string from the global configured XML. And next thing is we need to open the database connectivity and then perform the execution of the query. That's it. That is the only thing which we need to do so far that I'm going to go to the employee dot feature and let's uncommanded this definition and I'm going to implement this definition in my extended stepdads. Yes. So I'm going to add this definition right here. I have already written the code for it, so I'm just going to paste it. Basically right now it is missing a reference for the settings glass and squeakily out of this. So on. It's trying to do this. We are going to pass Equidae here, delete from employees where the name is equal to the employee name and the employee name is going to be a regular expression because today the employee name is the user. Tomorrow you can apply some other name. So every time we cannot keep on writing Steib definition for each and every user name. So I have parametrized ID meaning I'm using regular expression here and I'm passing the employee name as a barometer. And here is going to be the name of the employee, which are you going to supply? And it is basically going to call the execute query method, which we have in our database, our extension. So we're going to call or execute query method. So since it is an extension method, you can directly call the execute query method something like this, just your dart. And you can see that we have the execute gradiometer. Similarly, we have the Connect, DV Connect and DB close method. Right. Great. So now we have called this as well. And the last and the final thing which we need to do is before running this definition, we need to open the database connectivity, which we have not yet. So all our scenarios initialization is happening in our Hoog initialized answer. Yes. So I'm going to go to the Initialized Dark's, which is available in our Desco right here. After the initial setting is happening, we can call our DB open method. So I don't want this navigate site method. I just want to call the DB initialized. So far that what I'm going to do is I'm going to call our settings class and I'm going to call the app. Connection is equal to Sitting's dot application connection dot DB Connect. And here we need to parse the connection string. The connection string is actually sitting in Afghanistan string property, which we have already initialized. While we run this initialize setting method, it is going to internally call our configurator method. So everything is pretty much set. So our initialization offsetting is going to be happening here and our database is going to do this is going to happen here. Let's see the breakpoint here and we're going to debug this scenario, create employee with details. So I'm going to debunk this right to test now. I'm going to step over. All right. Now let's see what is the object that we have in our application connection properties. It should have opened the database connectivity by now. All right, so that it is is over, the database is employee and let's see the connection state, the state of the connection is open, which means the database is completely open right now, which is a good sign. So we have contacted the database, which is great. And then I'm going to run this particular of definition, and it actually came out very quickly because we did not do is step into this step. So let me go in here and I'm going to hit a break point here. Let's do it continue. All right. So now this committee is going to come in. It is nothing but the employee from the name and then it's a step or and then step into. So this is the connection that we have, which is nothing but our application connection. And the curious thing is something which you are passing that from employee by the name is auto user. So I'm going to go here and it is going to check if the connection is opened. So it is going to come to the if condition and is going to now end up to these second data adapter. Now it's going to run the quality. All right. The query and it's going to return the data set. Of course, it has nothing in there because it's a dated query. So it's going to close the connection and is going to return not for the dataset, which is great. So the queries executed. And now if you go to our database and if we execute the query, the auto user should have deleted by now, let's execute this. And you can see that the user is not available, which is a great sign, which means our database execution is actually happening behind the scenes. So now, if I hate to continue, you can see that it is going to perform the rest of operation, which is nothing but creation of any user. Let's see what is going to happen. I'm going to remove all the break points this time and it continues. So it's logging in as a user. And then it's going to create an employee and the auto user is created by now and now, if I try to execute the same scenario once again, you can see that this time it is going to delete the existing employee initially and then is going to create a new employee. So it is not going to duplicate the same employee again and again, because in the background definition, it is actually deleting the employee before creating one. And now you can see that in the step we don't have the art of user, even if it is going to create, it is going to have only one art to use at this time. So this is how we can make use of our database, help our extension library and perform database testing in our step definitions of specular. Thank you.