This post describes how to utilise SpecFlow to write unit tests that implement behaviour-driven development (BDD). Details on the BDD can be found on this wikipedia page. SpecFlow is simple to install through the Nuget package manager, more information can be found on the SpecFlow site.
SpecFlow tests comprise of two main elements: features and steps. In feature files, scenarios are written which together test all the behaviours of the feature. Each test will call methods in a step file, which is where the C# code resides and the actual.
In this blog post, we just look at a couple of very simple examples. In the future, there will be posts which go into more detail about the power of SpecFlow.
Below is an example feature file for getting all users from a database, titled Get all users.
At the top, the feature is defined subsequently a bit more detail is given. This is a suite of tests that will prescribe the behaviour for getting all users. The first scenario details the "happy path" - as an admin user, if I send a request to the server to get all users, Bill and Steph will return. SpecFlow unit tests use an in memory database to store data. Bill and Steph are setup in a seeder as can be seen below.
A UserSteps file is created to implement the test. The Binding attribute is added to the top of the file, to tie it to SpecFlow feature files. In this example, a instance of both TestAdministration and TestCurrentIdentity is initialised. These are the test implementations of the administration engine and current identity files respectively, but are not shown here for brevity.
The Givens, Whens, and Thens are added into the UserSteps file. The GivenIAmAnAdmin method uses the currentIdentity instance to make the current user an admin. The WhenIGetAListOfAllUsers method uses the instance of administration (an engine), and calls the GetUsers method on it. The final method, ThenTheFollowingUsersShouldBeReturned, is the most complex. Firstly, the table of users from the test is passed in, then the CreateSet method converts them to a list of User entities. Subsequently, the GetUsersResult is checked to confirm the correct number of users is returned. Finally, the GetUsersResult is checked to verify that all returned users have the same properties as expected from the test.
As previously mentioned, this blog post only scratches the surface of the power of SpecFlow. I hope to write more posts about utilising SpecFlow in the near future. When I do so, I'll post links to them below, for more efficient navigation between them.
SpecFlow tests comprise of two main elements: features and steps. In feature files, scenarios are written which together test all the behaviours of the feature. Each test will call methods in a step file, which is where the C# code resides and the actual.
In this blog post, we just look at a couple of very simple examples. In the future, there will be posts which go into more detail about the power of SpecFlow.
Below is an example feature file for getting all users from a database, titled Get all users.
A UserSteps file is created to implement the test. The Binding attribute is added to the top of the file, to tie it to SpecFlow feature files. In this example, a instance of both TestAdministration and TestCurrentIdentity is initialised. These are the test implementations of the administration engine and current identity files respectively, but are not shown here for brevity.
The Givens, Whens, and Thens are added into the UserSteps file. The GivenIAmAnAdmin method uses the currentIdentity instance to make the current user an admin. The WhenIGetAListOfAllUsers method uses the instance of administration (an engine), and calls the GetUsers method on it. The final method, ThenTheFollowingUsersShouldBeReturned, is the most complex. Firstly, the table of users from the test is passed in, then the CreateSet method converts them to a list of User entities. Subsequently, the GetUsersResult is checked to confirm the correct number of users is returned. Finally, the GetUsersResult is checked to verify that all returned users have the same properties as expected from the test.
As previously mentioned, this blog post only scratches the surface of the power of SpecFlow. I hope to write more posts about utilising SpecFlow in the near future. When I do so, I'll post links to them below, for more efficient navigation between them.
Further reading
ReplyDeleteWonderful blog. It is really informative to all.keep update more information about this
Software Testing Course in Bangalore
Software Testing Course in Hyderabad
Software Testing Course in Pune
Software Testing Training in Gurgaon
Great blog.thanks for sharing such a useful information
ReplyDeleteSalesforce CRM Training in Chennai
This post is so interactive and informative.keep update more information...
ReplyDeleteAngularjs Training in Tambaram
Angularjs Training in Chennai
In this digitally growing world, the demand for Software Testing has increased a lot, there are many institutes and training centers for its training but why you should choose SevenMentor for Software Testing Classes in Pune is because it provides:
ReplyDelete1. Training from basics to advance level and its course is designed for all kinds of students.
2. Mentors here are industry experts and are friendly.
3. Here you get multiple doubt-solving sessions.
4. You also get a certificate after successfully completing the course, which various MNCs accept.
visit our website: Software Testing Classes In Pune
Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing...
ReplyDeletevisit us: -swhizz
our services: -
Salesforce
DevOps
aws
Testing
Java