<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abhigyan Gautam</title>
    <description>The latest articles on Forem by Abhigyan Gautam (@ag2byte).</description>
    <link>https://forem.com/ag2byte</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F608346%2Ff6dcfa86-588e-47a0-bf34-3c980d5dcabd.png</url>
      <title>Forem: Abhigyan Gautam</title>
      <link>https://forem.com/ag2byte</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ag2byte"/>
    <language>en</language>
    <item>
      <title>Coding Practices I have learnt as a developer</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Tue, 04 Jul 2023 11:42:38 +0000</pubDate>
      <link>https://forem.com/ag2byte/coding-practices-i-have-learnt-as-a-developer-3phj</link>
      <guid>https://forem.com/ag2byte/coding-practices-i-have-learnt-as-a-developer-3phj</guid>
      <description>&lt;p&gt;Like a lot of developers, I ignored the importance of following coding practices when I started out. It seemed "cool" to just do stuff that works. But overtime, I have realised that these practices help improve the quality of code by making it more readable and efficient. Properly structured and documented code is easier to understand, debug, and maintain, even when multiple developers are working on the same project. Additionally, coding practices encourage the use of efficient algorithms and data structures, leading to optimised performance and reduced resource consumption. So here are a few things I have picked up over my time coding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Name your variables
&lt;/h3&gt;

&lt;p&gt;The single most important practice is naming your variables properly, or at least in a way that makes sense to your code. Check out the code below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WnSkuCfP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96e7eogx1bsw2xd28irf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WnSkuCfP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96e7eogx1bsw2xd28irf.jpg" alt="wrong variables" width="668" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The block of code works, but imagine seeing this somewhere in a large codebase and staring at it for very long time. Yes, it would make no sense when you come back. Instead using a readable variable name would solve so much.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BHsnW4Y_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssvkh83xwhyw0h6zt5eq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BHsnW4Y_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssvkh83xwhyw0h6zt5eq.jpg" alt="correct variables" width="800" height="358"&gt;&lt;/a&gt;&lt;br&gt;
The above change makes the code so much more readable. The naming convention can be decided, but follow that consistently&lt;/p&gt;

&lt;h3&gt;
  
  
  Structure your code
&lt;/h3&gt;

&lt;p&gt;In the above example, we have three different variables which can be thought of properties of one single object, in this case probably invoice. It is highly possible that same structure can be reused later in the code. It is better to make a structure to contain this data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MZQvPBd6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vwx40htzozwr9uydru2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MZQvPBd6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vwx40htzozwr9uydru2.jpg" alt="struct using dict" width="800" height="520"&gt;&lt;/a&gt;&lt;br&gt;
Using a dictionary to make an object makes it more readable and efficient than creating same variables over and over. A better way to do this is using Class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r2hups5g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1ygqaoak5hrn8gtrlc8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r2hups5g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1ygqaoak5hrn8gtrlc8.jpg" alt="STRUCT USING CLASS" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, avoid overuse of this as it makes the code bulky. &lt;/p&gt;

&lt;h3&gt;
  
  
  Comment your code
&lt;/h3&gt;

&lt;p&gt;Comments are very important. When dealing with large codebases, they become a necessity to understand the underlying architecture. These become very helpful in refactoring code later on and understanding logic. Adding documentation comments in functions increase readability of code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kGS8hUZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdpfy2m1i51njyvitczf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kGS8hUZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdpfy2m1i51njyvitczf.jpg" alt="code comments" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Review
&lt;/h3&gt;

&lt;p&gt;Its important to take suggestions and feedback from peers to maintain the quality of code. Code review also helps adding things one might missed during implementation. Using collaboration, code can also be optimised.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test your code
&lt;/h3&gt;

&lt;p&gt;This is something that I have recently learnt the importance of. Testing your code against all possible edge cases is important. This ensures the correctness of functionality being implemented against all scenarios. Plus, writing tests means other developers get the opportunity to check their code as per the standard required, so it's a win-win. For an example, check out &lt;a href="https://dev.to/ag2byte/series/23412"&gt;this&lt;/a&gt; series. &lt;/p&gt;

&lt;p&gt;What are some practices you use? Let me know in comments.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Get your apps up and running in no time</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Wed, 28 Jun 2023 08:23:18 +0000</pubDate>
      <link>https://forem.com/ag2byte/get-your-apps-up-and-running-in-no-time-1ip2</link>
      <guid>https://forem.com/ag2byte/get-your-apps-up-and-running-in-no-time-1ip2</guid>
      <description>&lt;p&gt;For a lot of developers, the tedious task is to setup the initial project. If you are just starting with a new technology, it becomes a hassle. This might lead to issues in the project repo later on. One way to make sure that things are setup just fine is to use a boilerplate and &lt;a href="https://aspnetboilerplate.com/"&gt;ASP.NET boilerplate&lt;/a&gt; is one of them.&lt;br&gt;
It is a general purpose application framework designed for modern web applications which provides a strong code template to get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of ASP.NET BoilerPlate
&lt;/h3&gt;

&lt;p&gt;The following are some advantages of using ASP.NET BoilerPlate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It gets your app setup and running in no time. It takes care of all the initial setup required including authentication, authorization, logging, exception handling, auto mapping, localization, multi-tenancy along with features like dependency injection, unit of work etc..&lt;/li&gt;
&lt;li&gt;It offers a module based approach, in which the modules can be used in a plug-and-play way.&lt;/li&gt;
&lt;li&gt;It has a well written documentation.&lt;/li&gt;
&lt;li&gt;It offers compatibility with all modern frameworks like React, Angular, Vue etc...&lt;/li&gt;
&lt;li&gt;It is open source and the free version has a lot of features. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using ASP.NET boilerplate
&lt;/h3&gt;

&lt;p&gt;To download the boiler plate code for your application, visit &lt;a href="https://aspnetboilerplate.com/Templates"&gt;ASP.NET template&lt;/a&gt;. Choose a template as per your requirement. For this article, let's choose React.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f4L72kdz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67oocyfuvyfjg2pf1zou.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f4L72kdz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67oocyfuvyfjg2pf1zou.jpg" alt="ASP.NET boilerplate template" width="800" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you download the project, you can see files for both your backend(.NET Core 7) and frontend (React). &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Sz1r_Ye--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo17t448fe45n9ws1d4n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Sz1r_Ye--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo17t448fe45n9ws1d4n.jpg" alt="folder structure" width="766" height="1604"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From here you can just install the packages for both and run the application. That's it!! Your app is up and running 🎉.&lt;/p&gt;

&lt;p&gt;Learn more about this at &lt;a href="https://aspnetboilerplate.com/"&gt;ABP&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@chuttersnap?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;CHUTTERSNAP&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/eH_ftJYhaTY?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>development</category>
      <category>aspdotnet</category>
    </item>
    <item>
      <title>Arrange.Act.Assert.</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Mon, 19 Jun 2023 11:59:30 +0000</pubDate>
      <link>https://forem.com/ag2byte/arrangeactassert-1kia</link>
      <guid>https://forem.com/ag2byte/arrangeactassert-1kia</guid>
      <description>&lt;p&gt;We will now write tests for the &lt;code&gt;Fruit&lt;/code&gt; Controller. As discussed before there are three steps that make up a unit test - Arrange, Act, Assert. We will go through each of these steps in this article. First create an Empty class in &lt;code&gt;RestTestUNIT&lt;/code&gt; project, let's call it &lt;code&gt;FruitControllerTest.cs&lt;/code&gt;. Add the following to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;RestTestUNIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;RestTest.Service&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;RestTest.Controllers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FruitControllerTest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;FruitBAsketController&lt;/span&gt; &lt;span class="n"&gt;fruitbasketcontroller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SetUp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetFruitsTest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Pass&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In .NET, we can associate functions inside the class with different testing tags. Some of these are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;OneTimeSetup&lt;/code&gt; - to define logic that run only once while running the Tests.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Setup&lt;/code&gt; - to definde code that must be executed before each testcase&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Test&lt;/code&gt; - marks a function as test function&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TestCase()&lt;/code&gt; - used to pass parameters to the test functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Arrange
&lt;/h3&gt;

&lt;p&gt;We need to provide the input parameters and the expected result in this step. We have a very simple api with just static data instead of data from a database, but in real applications this is not the case. For testing there, it is not viable to make database requests for each test. Thus there we use something called as Mock Response. We will try to use the same approach here as well. To mock data, we will use a package called &lt;code&gt;Moq&lt;/code&gt;. Go ahead and install Moq from Nuget manager. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F40OUYcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zzz2v0sdgkltor86741.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F40OUYcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zzz2v0sdgkltor86741.jpg" alt="installing moq" width="800" height="502"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets create some responses now. We will test the following cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When no argument is passed : this should return a list of fruits with no filter&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;fruitname&lt;/code&gt; is passed: this should only return a list of fruits with this name&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;fruitname&lt;/code&gt; and &lt;code&gt;sorted&lt;/code&gt; is passed: this should return fruit list with the same name and state of &lt;code&gt;sorted&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;fruitname&lt;/code&gt; is not present : this should return a &lt;code&gt;BadRequest&lt;/code&gt; response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add the following objects to the &lt;code&gt;FruitControllerTest&lt;/code&gt; class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;allfruits&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"
[
  {
    'name': 'Apple',
    'price': 43,
    'sorted': false
  },
  {
    'name': 'Litchi',
    'price': 32,
    'sorted': false
  },
  {
    'name': 'Litchi',
    'price': 32,
    'sorted': true
  },
  {
    'name': 'Mango',
    'price': 32,
    'sorted': false
  },
  {
    'name': 'Pineapple',
    'price': 26,
    'sorted': false
  },
  {
    'name': 'Apple',
    'price': 20,
    'sorted': false
  }
]"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;onlyapple&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"[
  {
    'name': 'Apple',
    'price': 43,
    'sorted': false
  },
  {
    'name': 'Apple',
    'price': 20,
    'sorted': false
  },
  {
    'name': 'Apple',
    'price': 32,
    'sorted': true
  }
]"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;onlytrueapple&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"[
  {
    'name': 'Apple',
    'price': 32,
    'sorted': true
  }
]"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;fruitList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will use these as the mocked responses. Since the data is returned from &lt;code&gt;GetFruits()&lt;/code&gt; in the &lt;code&gt;FruitService&lt;/code&gt; class, We will mock the response for this Service. For that, we will use the interface &lt;code&gt;IFruitService&lt;/code&gt; as interface only contains the declaration of the function and not its implementation.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;FruitBasketController&lt;/code&gt; add another constructor that initialises the controller with the interface instead of the actual service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;FruitBAsketController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;fruitServiceObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fruitservice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IFruitService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;fruitServiceObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will use this constructor for our mocked response. In the &lt;code&gt;Setup()&lt;/code&gt; we will setup our controller for testing. Add the following in the &lt;code&gt;Setup()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IFruitService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fruitmock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SetUp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fruitmock&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IFruitService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;fruitbasketcontroller&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FruitBAsketController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitmock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are setting up such that the fruitbasketcontroller will now refer the &lt;code&gt;fruitmock&lt;/code&gt; object now when calling the service, instead of the actual definition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Act
&lt;/h3&gt;

&lt;p&gt;In this step, we will call our functions to return functions to return the above responses based on different test cases. Add the following function in &lt;code&gt;FruitControllerTest.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Kiwi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetFruitsTest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Fruit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;?&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// get all fruits&lt;/span&gt;
                &lt;span class="n"&gt;fruitList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allfruits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"Apple"&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;fruitname&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// get only apples&lt;/span&gt;
                &lt;span class="n"&gt;fruitList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;onlyapple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="c1"&gt;// sorted is true&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"Apple"&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;fruitname&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// only true apple&lt;/span&gt;
                    &lt;span class="n"&gt;fruitList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;onlytrueapple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="c1"&gt;//if the fruit does not exist fruitList is empty&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Fruit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;fruitmock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetFruits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;Returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fruitbasketcontroller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAllFruits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The different values for parameters are passed through the &lt;code&gt;TestCase&lt;/code&gt; tag. Using this parameters, we populate the list &lt;code&gt;fruitList&lt;/code&gt; which is then converted into &lt;code&gt;List&amp;lt;Fruit&amp;gt;&lt;/code&gt; using &lt;code&gt;JsonConvert&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Since we are using a mock object for &lt;code&gt;FruitService&lt;/code&gt;, we can set it up to return response based on the above steps. This means it does not go into actual definition of functions in &lt;code&gt;FruitService&lt;/code&gt; class but just mocks the response. &lt;/p&gt;

&lt;h3&gt;
  
  
  Assert
&lt;/h3&gt;

&lt;p&gt;In the third stage, we will assert the responses ,i.e, we will validate the response returned from the function. Add the following Assertions just below the above code in &lt;code&gt;GetFruitsTest()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;       &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fruitbasketcontroller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAllFruits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="n"&gt;IStatusCodeActionResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;okObject&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;OkObjectResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fruitList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;okObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Fruit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Fruit&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;True&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StringComparison&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;True&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sorted&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="n"&gt;IStatusCodeActionResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;badRequest&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;BadRequestObjectResult&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;True&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;badRequest&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"Found Nothing"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the Service can return two types of responses with status code 200 and 400, we will make our assertions accordingly. And with that we have successfully written Unit Test for out controller. Go ahead and run these tests from Test Explorer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FyOMzLVw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qarqzfnn2oboqer105o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FyOMzLVw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qarqzfnn2oboqer105o.gif" alt="lets go" width="90" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>unittest</category>
      <category>testing</category>
    </item>
    <item>
      <title>Setting up the Project for Testing</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Mon, 19 Jun 2023 11:59:20 +0000</pubDate>
      <link>https://forem.com/ag2byte/setting-up-the-project-for-testing-4f0i</link>
      <guid>https://forem.com/ag2byte/setting-up-the-project-for-testing-4f0i</guid>
      <description>&lt;p&gt;In this demo, we will setup a basic .NET Project and add Unit Testing to it. To follow along, make sure you have Visual Studio installed on your machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  The project Setup
&lt;/h3&gt;

&lt;p&gt;You can clone the sample project from &lt;a href="https://github.com/ag2byte/SampleASP"&gt;Github&lt;/a&gt;. When you run it for the first time, it will automatically fetch the packages required and compile the code. It is a simple WebAPI Project with only one API currently. When you run the project, you will see a Swagger Open API screen as this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f3wg6rwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29kkn5pg6rmjzpuc43s6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f3wg6rwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29kkn5pg6rmjzpuc43s6.jpg" alt="Swagger" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a simple API which returns a List of Fruits based on the parameters passed to it. &lt;br&gt;
The &lt;code&gt;Fruit&lt;/code&gt; Class is defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Fruit&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The controller for this endpoint is defined as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.AspNetCore.Mvc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Xml.Linq&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;RestTest.Service&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Newtonsoft.Json&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;RestTest.Controllers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ApiController&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[controller]"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FruitBAsketController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ControllerBase&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;


    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IFruitService&lt;/span&gt; &lt;span class="n"&gt;fruitservice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;FruitBAsketController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fruitservice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FruitService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;HttpGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"GetAllFruit"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IActionResult&lt;/span&gt; &lt;span class="nf"&gt;GetAllFruits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;     
         &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Fruit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fruitList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fruitservice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetFruits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;fruitList&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;BadRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Found nothing"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;TotalCount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;TotalPrice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"metadata"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SerializeObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It takes two parameters &lt;code&gt;fruitname&lt;/code&gt; and &lt;code&gt;sorted&lt;/code&gt;. It returns list of Fruits based on these arguments(if present). The function for that is defined in &lt;code&gt;FruitService.cs&lt;/code&gt; as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Fruit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetFruits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;responseResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Fruit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt;&lt;span class="n"&gt;fruitList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;responseResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;responseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fruitname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StringComparison&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;responseResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;responseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;==&lt;/span&gt;&lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;responseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example response is shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kVVrb9Jd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf6z2t4d6irg4r7a2lch.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kVVrb9Jd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf6z2t4d6irg4r7a2lch.jpg" alt="exampleresponse" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up the Unit Testing Project
&lt;/h3&gt;

&lt;p&gt;Now we will setup the Unit Test Project for this Sample Project. Go to File &amp;gt; Add&amp;gt; Project and choose Nunit Test Project&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VfSOx-YE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6au5z5kfrra0hxju6cy8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VfSOx-YE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6au5z5kfrra0hxju6cy8.jpg" alt="nunittest" width="800" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the Projectname, here it is called &lt;code&gt;RestTestUNIT&lt;/code&gt;. Now we need to add the reference of the actual project in the test project. Right click on Dependencies in &lt;code&gt;RestTestUNIT&lt;/code&gt; and Add Project Reference. Select the original project from this and add the reference. It should now show up under dependencies of &lt;code&gt;RestTestUNIT&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--An-OJcFK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs8w1kkfcyjecta5be2v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--An-OJcFK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs8w1kkfcyjecta5be2v.jpg" alt="Add reference" width="800" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>unittest</category>
      <category>dotnet</category>
      <category>testing</category>
    </item>
    <item>
      <title>What Exactly is Unit Testing?</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Mon, 19 Jun 2023 11:59:08 +0000</pubDate>
      <link>https://forem.com/ag2byte/what-exactly-is-unit-testing-47j5</link>
      <guid>https://forem.com/ag2byte/what-exactly-is-unit-testing-47j5</guid>
      <description>&lt;p&gt;One of the things that developers don't like doing is writing test cases for their code, yet it is one of the most important steps that ensures code quality of small units of the project.  In this series we will look into what Unit Tests are and how can we setup a simple Unit Testing module in C#. Please note that there are several ways to do it, I am just trying to share what I've learnt. Feel free to add to it!&lt;/p&gt;

&lt;p&gt;The main objective of unit testing is to isolate and validate each unit of code independently, verifying that it behaves as expected and produces the desired output given specific inputs. Since the units are tested independently, developers can detect the bugs early in the development process making it easier to identify and fix before they have a huge impact on the project.&lt;/p&gt;

&lt;p&gt;For example, let's consider a simple function as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Factorial is not defined for negative numbers."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above function &lt;code&gt;Factorial()&lt;/code&gt; is a unit in this case. For the function to return correct response, the following test cases can be thought of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Factorial of 0 is 1&lt;/li&gt;
&lt;li&gt;Factorial of positive number is correct.Example &lt;code&gt;Factorial(5)&lt;/code&gt; should return 120&lt;/li&gt;
&lt;li&gt;Factorial of Negative Number should throw Exception, in this case Argument Exception.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The general idea is that test cases like this should return the respective responses. This is called Assertion.&lt;/p&gt;

&lt;p&gt;In fact, the three main steps of Unit Test are :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrange&lt;/strong&gt; - The necessary preconditions and inputs for the Unit to be tested are prepared. In the above case the numbers to be tested are a part of this step&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Act&lt;/strong&gt; - The test is called using the above &lt;em&gt;arranged&lt;/em&gt; items. In the above case calling the function &lt;code&gt;Factorial()&lt;/code&gt; with the numbers represents this step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assert&lt;/strong&gt; - The observed output is compared with the expected output and the test is marked as correct or failed based on the output. In the above case &lt;code&gt;Factorial(5)&lt;/code&gt; gives 120 represents this step.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unit tests should cover various scenarios, including both expected and edge cases, to provide comprehensive coverage of the unit's behaviour. By having a suite of well-designed and thorough unit tests, developers gain confidence in their code and can easily detect regressions or unintended side effects when making changes or refactoring code.&lt;/p&gt;

&lt;h3&gt;
  
  
  How is this different from Integration Testing?
&lt;/h3&gt;

&lt;p&gt;Unit Test works on small parts of code at one time. Integration Testing is the end to end testing of these units all at once. Integration Testing gives the report of how where these so called units can work together while Unit Test give the accuracy of these individual units.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zQ35Wkpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtydytih3r2fna0uk3n4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQ35Wkpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtydytih3r2fna0uk3n4.gif" alt="unit-integrationtest" width="498" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>unittest</category>
      <category>dotnet</category>
      <category>testing</category>
    </item>
    <item>
      <title>Understanding Git branches</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Thu, 15 Jun 2023 20:28:47 +0000</pubDate>
      <link>https://forem.com/ag2byte/understanding-git-branches-3194</link>
      <guid>https://forem.com/ag2byte/understanding-git-branches-3194</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/ag2byte/what-actually-happens-when-you-git-36nf"&gt;last&lt;/a&gt; article we talked about internal working of Git. We saw the actual meaning of git commits and how git uses them to track different versions of stuff in the repository. &lt;/p&gt;

&lt;p&gt;One of the biggest things git offers is the ability to create branches. These are separate parallel paths carrying different versions of the repository, at times independent from each other. In this article we will look at what actually happens when you use branches. &lt;/p&gt;

&lt;h3&gt;
  
  
  A new branch
&lt;/h3&gt;

&lt;p&gt;So far we have only had one branch,i.e, &lt;code&gt;master&lt;/code&gt;. Go ahead and create a new branch using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; git branch canon
&amp;gt; git checkout canon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we check the &lt;code&gt;.git&lt;/code&gt; folder now, we have a new file called &lt;code&gt;canon&lt;/code&gt; in refs&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BDdJXqjh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7o8ee21ru2kunlt6vkz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BDdJXqjh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7o8ee21ru2kunlt6vkz.jpg" alt="canon in refs" width="576" height="192"&gt;&lt;/a&gt;&lt;br&gt;
The &lt;code&gt;refs&lt;/code&gt; folder basically tracks the different references throughout the repository. The &lt;code&gt;HEAD&lt;/code&gt; file contains the path to this &lt;code&gt;canon&lt;/code&gt; file as well. Now let's check the contents of the &lt;code&gt;canon&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GTy040Zh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1du1d1wtd635or0fbmc9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GTy040Zh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1du1d1wtd635or0fbmc9.jpg" alt="cat canon" width="800" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that this is the most recent commit in the &lt;code&gt;canon&lt;/code&gt; branch. Thus the repository tree looks something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OBaiNUls--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l44x09vedn7u73q31j3z.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OBaiNUls--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l44x09vedn7u73q31j3z.jpg" alt="branch structure 1" width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  New branch moves forward
&lt;/h3&gt;

&lt;p&gt;Lets make some changes in &lt;code&gt;testfile&lt;/code&gt; in the &lt;code&gt;canon&lt;/code&gt; branch and commit it. We now have three new files in the &lt;code&gt;objects&lt;/code&gt; folder&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--78Xv9H0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9elzl7qn82w831ru7tt7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--78Xv9H0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9elzl7qn82w831ru7tt7.jpg" alt="new files" width="686" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;git cat-file&lt;/code&gt;, we can deduce the relations between each of these files&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hU34-xSK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0k7zho20pmhnsw45jhk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hU34-xSK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0k7zho20pmhnsw45jhk.jpg" alt="git cat-file" width="800" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is similar to what we saw in the last article, &lt;code&gt;5d...&lt;/code&gt; is the commit, &lt;code&gt;51...&lt;/code&gt; is the tree originating from the previous commit(&lt;code&gt;f5...&lt;/code&gt;) and &lt;code&gt;23...&lt;/code&gt; is the blob where the actual content is present. The repository looks something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8VHHvXt0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l332xppouqzaptj854np.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8VHHvXt0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l332xppouqzaptj854np.jpg" alt="new tree" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that the &lt;code&gt;objects&lt;/code&gt; folder only contains this information and not the information of which branch this is on. That is maintained by the &lt;code&gt;HEAD&lt;/code&gt; file. Since the &lt;code&gt;HEAD&lt;/code&gt; points to the &lt;code&gt;canon&lt;/code&gt; and that contains the new commit, the &lt;code&gt;HEAD&lt;/code&gt; now points to the latest commit in &lt;code&gt;canon&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CMcDIJf5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw5mcjnbl5siyksljpqv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CMcDIJf5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw5mcjnbl5siyksljpqv.jpg" alt="canon cat-file" width="678" height="80"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Merging the new branch
&lt;/h3&gt;

&lt;p&gt;Let's merge the new branch in the master branch&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ git checkout master
❯ git merge canon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UwByVmmB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95su1876foc1hcuze8mv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UwByVmmB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95su1876foc1hcuze8mv.jpg" alt="New merge" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if we check the contents of &lt;code&gt;master&lt;/code&gt; in refs, we get &lt;code&gt;5d6808021c9dfd7bbe88fb8ac450e572982f79b7&lt;/code&gt;, which is the latest commit that is merged from the new branch. Thus the repository looks something like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--91VVWWJ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d80wdser3tiumicthxxi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--91VVWWJ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d80wdser3tiumicthxxi.jpg" alt="new tree" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thus we can see how git maintains the reference of the branches along with the contents of it. This is what makes git so powerful. Having a tree structure means git can navigate to any specific commit in any specific branch. &lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>git</category>
      <category>development</category>
    </item>
    <item>
      <title>What actually happens when you git?</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Mon, 29 May 2023 11:17:40 +0000</pubDate>
      <link>https://forem.com/ag2byte/what-actually-happens-when-you-git-36nf</link>
      <guid>https://forem.com/ag2byte/what-actually-happens-when-you-git-36nf</guid>
      <description>&lt;p&gt;Git is by far one of the most useful tools if you are a software developer. Git is a version control system used by developers to keep track of changes made to a project's codebase. It allows multiple people to collaborate on the same codebase without overwriting each other's work.&lt;/p&gt;

&lt;p&gt;But what’s even interesting is how Git manages to maintain the different versions of the code, down to an individual line. In this article we will take a look into how git actually handles the version control system&lt;/p&gt;

&lt;h3&gt;
  
  
  The .git folder
&lt;/h3&gt;

&lt;p&gt;Whenever you initialise a project with &lt;code&gt;git init&lt;/code&gt;, a hidden folder called &lt;code&gt;.git&lt;/code&gt; . This is where git stores information regarding the project, git configurations and information about the versions. Let’s initialise a folder with git and see the contents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h52n0izw7r6t07qa5wj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h52n0izw7r6t07qa5wj.jpg" alt="Git Folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are the contents of the &lt;code&gt;.git&lt;/code&gt; folder. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;config&lt;/code&gt; contains details about the repository configurations like &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt; for the project etc. This file is overwritten with new properties.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;description&lt;/code&gt; contains the description of the repository&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HEAD&lt;/code&gt; This file maintains the reference to the current branch. At the moment it must be the master branch.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;refs&lt;/code&gt; stores references to all the branches.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;objects&lt;/code&gt; stores the data of the Git objects which contains contents of all the files checked in, commits etc..&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;hooks&lt;/code&gt; contains shell script commands that are executed post git commands.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;info&lt;/code&gt; contains information about the repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Git’s SHA1 hashing
&lt;/h3&gt;

&lt;p&gt;Git stores data in the form of blob objects. Every blob in git is hashed with &lt;code&gt;SHA 1&lt;/code&gt; which are 20 bytes, represented by 40 hexadecimal characters. We can generate hashes for any content. For example, the &lt;code&gt;SHA 1&lt;/code&gt; hash for “&lt;strong&gt;Superman&lt;/strong&gt;” is &lt;code&gt;5f42cf3e4992beffcd80266227d529427adb7a2d&lt;/code&gt;. There is one and only one hash for the content “&lt;strong&gt;Superman&lt;/strong&gt;”. You can check this for yourself using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ echo "Superman" | git hash-object  --stdin
5f42cf3e4992beffcd80266227d529427adb7a2d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we change the content, we get a completely new hash&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ echo "SuperMan" | git hash-object  --stdin
3b552a73712ce7111a4aa6a600f19700ae378f7a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the basis of what git does. It tracks the changes by generating a different hash each tome a change comes in.  &lt;/p&gt;

&lt;h3&gt;
  
  
  A new commit
&lt;/h3&gt;

&lt;p&gt;Now lets add a file called &lt;code&gt;testfile&lt;/code&gt; to this repo and commit it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ touch testfile
❯ git add testfile
❯ git commit -m "added test file"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvebvxir12vtddaaenqc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvebvxir12vtddaaenqc.jpg" alt="First Commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now notice the commit number: &lt;code&gt;c36ed1c&lt;/code&gt;. This is the blob object created for this commit. Git maintains the versions using something similar to a file system. It stores the content of the object, the commits in the form of a blob object. The difference between blob and file is that blob stores only the content, while file can store the metadata as well. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;c36ed1c&lt;/code&gt; is just the first seven letters of the actual hashed name. If we now go to the git folder we see the following:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F53vpupbdlrqgz7t9hajw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F53vpupbdlrqgz7t9hajw.jpg" alt="Git folder"&gt;&lt;/a&gt;&lt;br&gt;
Here you can see the full hashed name under folder &lt;code&gt;c3&lt;/code&gt; . The string starting with &lt;code&gt;c3&lt;/code&gt; and ending with &lt;code&gt;2b&lt;/code&gt; is the full hash of the commit blob. But what about the other hashes? We did not create them. Well, we did create them… sort of. Let me explain.&lt;/p&gt;

&lt;p&gt;Type the following command in the root of the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ git cat-file c36ed1cabb3565974f8846391f6ed59959f2d02b -p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command shows the content of the hash, in this case it is the commit object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5x5egq3ugoegybau8re.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5x5egq3ugoegybau8re.jpeg" alt="git cat-file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the content of this blob is &lt;code&gt;added test file&lt;/code&gt; which was our content for the commit message. It contains a reference to other hash &lt;code&gt;c6dc3ef...&lt;/code&gt; which contains the contents about the tree. &lt;/p&gt;

&lt;p&gt;Again if we use &lt;code&gt;git cat-file&lt;/code&gt; in this hash, we get the following&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhemdnc24xaycqppjeb58.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhemdnc24xaycqppjeb58.jpg" alt="tree cat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time it contains a reference to the &lt;code&gt;testfile&lt;/code&gt; and is stored as hash &lt;code&gt;e69de29...&lt;/code&gt; . Thus now we have the filename, the content of the file and the tree all stored in git in their hashed format. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6dzjarqwv7hod44v1kf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh6dzjarqwv7hod44v1kf.jpg" alt="internal view"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Making changes
&lt;/h3&gt;

&lt;p&gt;Now lets add some content in &lt;code&gt;testfile&lt;/code&gt;. Adding a simple text , “&lt;strong&gt;this is a test file&lt;/strong&gt;” in the file. Now when we do &lt;code&gt;git status&lt;/code&gt; we see that this file has been modified because the respective generated hashes do not match with what is present in the &lt;code&gt;.git&lt;/code&gt; folder. This can be tracked down to individual line as git maintains a hash of the content at the lowest level (as seen above). &lt;/p&gt;

&lt;p&gt;Lets commit this change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ git add testfile
❯ git commit -m "second commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time three new subdirectories are added under &lt;code&gt;objects&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0mr4rvgpyvvo6gys8l3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0mr4rvgpyvvo6gys8l3.jpg" alt="new files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again using &lt;code&gt;git cat-file&lt;/code&gt;, we can see the contents of these new hashes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flcpb6oie854utue320g8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flcpb6oie854utue320g8.jpg" alt="new cat-file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, we have a new property for this hash - &lt;code&gt;parent&lt;/code&gt; . Since git commits work in the form of trees and each commit is a node, the previous commit becomes the parent of this commit. Therefore &lt;code&gt;c36ed...&lt;/code&gt; is the parent of the commit &lt;code&gt;f51e8...&lt;/code&gt; . Thus it becomes easier to track commits and their history is represented in the form of the relation between nodes. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Now we know how git maintains a version report of every single piece of content in the project. Git can then use these generated hashes and the tree to pinpoint exactly the content that is required down to the smallest level.&lt;/p&gt;

&lt;p&gt;Thanks for reading! 👋&lt;/p&gt;

&lt;p&gt;Cover photo by Photo by &lt;a href="https://unsplash.com/@praveentcom?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Praveen Thirumurugan&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>versioncontrol</category>
    </item>
    <item>
      <title>Goodbye create-react-app</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Tue, 21 Mar 2023 18:43:06 +0000</pubDate>
      <link>https://forem.com/ag2byte/create-react-app-is-officially-dead-h7o</link>
      <guid>https://forem.com/ag2byte/create-react-app-is-officially-dead-h7o</guid>
      <description>&lt;p&gt;React developer team recently removed &lt;code&gt;create-react-app&lt;/code&gt; from the official documentation. This means it is no longer the default method of setting up a new project in React. According to &lt;a href="https://github.com/reactjs/react.dev/pull/5487" rel="noopener noreferrer"&gt;this&lt;/a&gt; pull request on Github, &lt;code&gt;create-react-app&lt;/code&gt; is finally gone.&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem with CRA
&lt;/h3&gt;

&lt;p&gt;For far too long,&lt;code&gt;create-react-app&lt;/code&gt; or CRA has had problems with its performance. It is slow and bulky compared to the modern methods. The initial setup is quite bulky as there are a lot of dependencies to be installed. It also is outdated as the dependencies themselves suffer from warnings during installation. These problems have troubled developers for far too long. Since CRA was the officially supported way, beginners had a hard time solving these issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the alternatives?
&lt;/h3&gt;

&lt;p&gt;There are various ways to setup a new React project. In fact, the official documentation now mentions &lt;a href="https://react.dev/learn/start-a-new-react-project" rel="noopener noreferrer"&gt;frameworks like NEXT-JS, Remix etc for beginners&lt;/a&gt;. In this post however, we will look into using &lt;em&gt;Vite&lt;/em&gt; to setup our React app in under a minute.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Vite?
&lt;/h3&gt;

&lt;p&gt;Vite is one of the fastest ways to start a project in react. It has faster server start time. It has better compatibility with plugins. It supports TypeScript, has better dependency resolving features out of the box. React projects created from Vite are just 20% the size of that created by CRA. You can read more about Vite &lt;a href="https://vitejs.dev/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a new React project using Vite
&lt;/h3&gt;

&lt;p&gt;Let's create a new React project using Vite. Run the following in the folder where you want your new app.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm create vite@latest


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Funym50u8fhr5om4leunk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Funym50u8fhr5om4leunk.jpg" alt="Vite setup" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name your project and select React from the type of project. Next select variant, here we are using JavaScript. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0hhtifqvjvfwdlecc5vm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0hhtifqvjvfwdlecc5vm.jpg" alt="React Variant" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And it is done! Seriously, that's it. All of this takes less than a minute to setup. &lt;br&gt;
You can now run the app using&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cd &amp;lt;project-root&amp;gt;
npm i
npm run dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Your app is up and running&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4hl543me0uwnnroljexc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4hl543me0uwnnroljexc.jpg" alt="React app running" width="800" height="809"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations!🎉🎉&lt;/p&gt;

</description>
      <category>react</category>
      <category>createreactapp</category>
      <category>vite</category>
    </item>
    <item>
      <title>Connect your local resources from inside your Docker Container</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Wed, 15 Mar 2023 05:47:40 +0000</pubDate>
      <link>https://forem.com/ag2byte/connect-your-local-resources-from-inside-your-docker-container-h4k</link>
      <guid>https://forem.com/ag2byte/connect-your-local-resources-from-inside-your-docker-container-h4k</guid>
      <description>&lt;p&gt;Docker containers are like magic, allowing you to run full fledged applications without going through all the steps and dependencies. The best part is that the experience is same throughout. But sometimes you want to connect to the local resources from inside the container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use case
&lt;/h2&gt;

&lt;p&gt;If you are new to Docker, check out &lt;a href="https://dev.to/ag2byte/creating-your-first-docker-container-a-beginners-guide-5a0o"&gt;Creating your first Docker Container - A beginner's Guide&lt;/a&gt;. Suppose you have a custom script that creates a database, creates the tables and the relationships and adds data. Since the script may require some dependencies, the best way to ensure that there are no issues for others is to dockerize the entire setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Folder Structure
&lt;/h2&gt;

&lt;p&gt;The following is a simple code structure for the above use case.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

- dbstuff.py
- requirements.txt
- Dockerfile
- requirements.txt
- .env


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dbstuff.py&lt;/code&gt; contains the custom python script that handles all the DML and DDL parts.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;requirements.txt&lt;/code&gt; has the dependencies used by the project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Dockerfile&lt;/code&gt; contains the layers required to create the image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt; contains the environment variables which include database connection details
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DATABASE_USER=devto&lt;br&gt;
DATABASE_PASSWORD=dbpass&lt;br&gt;
DATABASE_HOST=localhost&lt;br&gt;
DATABASE_NAME=devtotest&lt;br&gt;
...&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## Dockerize the image
Once you create a docker image using `docker build` and run the container using `docker run`, you will face an issue connecting tot the database. This is because the container is trying to connect to the database instance inside it and not on the local machine.


![Normal Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fj5w1jrpqqpu7gv2kww.jpg)

## The solution
We need to tell docker to connect to the host's local resources rather than the container's resources. We can do this by changing the .env as follows
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...&lt;br&gt;
DATABASE_HOST=host.docker.internal&lt;br&gt;
...&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;![Host Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bclomlpxgr8rqgqzm2ta.jpg)
This will now point to the `localhost` of the host. Problem solved!

Create another image and run the container and this should now work as intended! Congratulations 🎉🎉! You have successfully connected to your local Database from inside your docker container

Photo by [Manuel Geissinger] (https://www.pexels.com/photo/black-server-racks-on-a-room-325229/)



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>docker</category>
      <category>python</category>
      <category>containers</category>
      <category>database</category>
    </item>
    <item>
      <title>Creating your first Docker Container - A beginner's Guide</title>
      <dc:creator>Abhigyan Gautam</dc:creator>
      <pubDate>Sun, 05 Mar 2023 08:57:45 +0000</pubDate>
      <link>https://forem.com/ag2byte/creating-your-first-docker-container-a-beginners-guide-5a0o</link>
      <guid>https://forem.com/ag2byte/creating-your-first-docker-container-a-beginners-guide-5a0o</guid>
      <description>&lt;p&gt;Picture this, you built a cool homepage for a website and it is working exactly as you want on your machine. You share it with someone else to get their feedback, but it just won't work the same way. Believe it or not, this happens all the time in the software development world. Docker is a tool that helps you solve this.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Docker?
&lt;/h3&gt;

&lt;p&gt;Docker is a powerful tool for creating, deploying, and managing software applications in containers. A &lt;strong&gt;container&lt;/strong&gt; is a lightweight, standalone, and executable package that includes everything needed to run the software, such as code, libraries, and dependencies. Think of it as a complete bundle with all the inner working hidden from the outer world. Docker can be used to create these containers which are independent of the host's environment, thus providing the same experience across systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does this work?
&lt;/h3&gt;

&lt;p&gt;Containers have their standalone filesystem thanks to the &lt;strong&gt;images&lt;/strong&gt; they are built on. An Image is a building block of the container and hence houses everything that is required for the container to run - the code, the filesystem, dependencies.&lt;br&gt;
Docker packages these all in one image which can then be used to create the containers.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will containerise a simple Python application. Note that the same process can be used to create containers of any size.&lt;/p&gt;
&lt;h3&gt;
  
  
  Project Structure
&lt;/h3&gt;

&lt;p&gt;Go ahead and create a new folder on your system. We will call this &lt;code&gt;docker-test&lt;/code&gt;. Create a python file called &lt;code&gt;pydock.py&lt;/code&gt;. Add the following sample code to the python file or feel free to use your own code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;faker&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Faker&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tabulate&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tabulate&lt;/span&gt;
&lt;span class="n"&gt;fake&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Faker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# print 10 fake names and addresses
&lt;/span&gt;&lt;span class="n"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;address&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="c1"&gt;# generating the names and addresses
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fake&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fake&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;address&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# print in tabular format
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tabulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;firstrow&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;tablefmt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;fancy_grid&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple application that generates fake names and addresses in a tabular format. Notice that we are using external libraries - &lt;code&gt;faker&lt;/code&gt; and &lt;code&gt;tabulate&lt;/code&gt; for the same.&lt;/p&gt;

&lt;p&gt;Now create a &lt;code&gt;.dockerignore&lt;/code&gt; file in the root of the project. Use this to add the files that you want to ignore while creating an image. Your folder structure should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-test
-- pydock.py
-- .dockerignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker Setup
&lt;/h3&gt;

&lt;p&gt;Go ahead and install &lt;strong&gt;Docker Desktop&lt;/strong&gt; for your system from &lt;a href="https://docs.docker.com/get-docker/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. While you are at it, create an account on &lt;a href="https://hub.docker.com/" rel="noopener noreferrer"&gt;dockerhub&lt;/a&gt; as well. &lt;/p&gt;

&lt;p&gt;Once docker desktop is installed, you can open the application. This will automatically start a &lt;code&gt;docker daemon&lt;/code&gt; in background. &lt;/p&gt;

&lt;h3&gt;
  
  
  Dockerfile - where the magic lives
&lt;/h3&gt;

&lt;p&gt;Next, create a file called &lt;code&gt;Dockerfile&lt;/code&gt; in the root of the project. This defines how the image will be created and what will be used to create the image. The project structure should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-test
-- pydock.py
-- .dockerignore
-- Dockerfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit the contents of the &lt;code&gt;Dockerfile&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; python:3.8-slim-buster&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;Faker tabulate

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [ "python3","pydock.py"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's go over the contents of this &lt;code&gt;Dockerfile&lt;/code&gt; from top to bottom. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;FROM&lt;/code&gt; instruction specifies what kind of image we want to create. We can pull pre-built images from &lt;strong&gt;DockerHub&lt;/strong&gt;, including full-scale Operating Systems. However in this case, we just need to build a python image so we are using &lt;code&gt;python:3.8-slim-buster&lt;/code&gt;. You can choose any python image that are mentioned &lt;a href="https://hub.docker.com/_/python" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;WORKDIR&lt;/code&gt; instruction specifies the working directory in the container's filesystem. Think of this as the path where your image will sit and run in the container. In this case it is &lt;code&gt;/app&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;COPY&lt;/code&gt; instruction lets you copy files from your machine to the image. The syntax is &lt;code&gt;COPY &amp;lt;source&amp;gt; &amp;lt;target&amp;gt;&lt;/code&gt;, where &lt;code&gt;&amp;lt;source&amp;gt;&lt;/code&gt; is the file path on your local machine and &lt;code&gt;&amp;lt;target&amp;gt;&lt;/code&gt; is the file path on the container starting from the working directory. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN&lt;/code&gt; instruction is used to execute commands during the build process of a Docker image. Here we are using this to install our dependencies - &lt;code&gt;Faker&lt;/code&gt; and &lt;code&gt;tabulate&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;CMD&lt;/code&gt; instruction is used to specify the default command to be executed when a container is started from the image. Notice that &lt;code&gt;CMD&lt;/code&gt; uses an array format to define the commands while &lt;code&gt;RUN&lt;/code&gt; uses a simple string format&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; Docker images are a set of layers. Every instruction in a &lt;code&gt;Dockerfile&lt;/code&gt; creates a layer. These layers are cached internally for faster image builds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building a Docker image
&lt;/h3&gt;

&lt;p&gt;Once you are done with the &lt;code&gt;Dockerfile&lt;/code&gt;, we can move on to create your first Docker image. Open terminal in the root of your project. You can check if Docker is running using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To build an image, run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build  -t &amp;lt;username&amp;gt;/&amp;lt;image-name&amp;gt;:&amp;lt;tag&amp;gt; .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can call the image whatever you want, but the following conventions are generally used.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;-t&lt;/code&gt; option lets you create a tag for you image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;username&amp;gt;&lt;/code&gt; is your docker hub username.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;image-name&amp;gt;&lt;/code&gt; is the name you want to give to the image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;tag&amp;gt;&lt;/code&gt; is like the version number for the image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.&lt;/code&gt; is the path where &lt;code&gt;Dockerfile&lt;/code&gt; is present, here we have it in the root of the project&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Docker starts creating image layers from the instructions mentioned in the &lt;code&gt;Dockerfile&lt;/code&gt;. Once done, you can check your built image in Docker Desktop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running the container
&lt;/h3&gt;

&lt;p&gt;To run the image inside a container, we will use the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run &amp;lt;username&amp;gt;/&amp;lt;image-name&amp;gt;:&amp;lt;tag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run a container from the image that we built in the above process. You should see a similar output once you run the above command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn09itskcivh84b6pqx2b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn09itskcivh84b6pqx2b.jpg" alt="Output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations🎉🎉! You have created your first Docker Container successfully. You can check the container in Docker Desktop and even publish this to Docker Hub. You can share this image with others and they can simply run this as a standalone application. No installation fuss, no dependency issues, nothing! They won't even know what's inside the container but the container works as required!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;CREDITS&lt;/em&gt;&lt;br&gt;
Cover photo by &lt;a href=""&gt;Venti&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/1cqIcrWFQBI?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>python</category>
      <category>containers</category>
    </item>
  </channel>
</rss>
