<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tests Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/tests/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/tests/</link>
	<description>Building real-world software with AI 🤖</description>
	<lastBuildDate>Sun, 19 Jun 2022 13:07:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>tests Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/tests/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>Write Test Progress To The Console With NUnit</title>
		<link>https://www.codejourney.net/write-test-progress-to-the-console-with-nunit/</link>
					<comments>https://www.codejourney.net/write-test-progress-to-the-console-with-nunit/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 19 Jun 2022 13:07:28 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit tests]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4070</guid>

					<description><![CDATA[<p>I recently needed to write test progress to the console with NUnit. The task we want to solve here is basically the TODO part of this snippet: The title picture of this article shows the end result. If you want to know the solution, keep reading 🙂 Context The context of this need is very&#8230;</p>
<p>The post <a href="https://www.codejourney.net/write-test-progress-to-the-console-with-nunit/">Write Test Progress To The Console With NUnit</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">I recently needed to write test progress to the console with NUnit. The task we want to solve here is basically the <em>TODO</em> part of this snippet:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist116902181" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-nunit_test_progress_todo-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="NUnit_test_progress_todo.cs content, created by dsibinski on 04:54AM on June 19, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="NUnit_test_progress_todo.cs">
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-nunit_test_progress_todo-cs-LC1" class="blob-code blob-code-inner js-file-line">[TestFixture]</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-nunit_test_progress_todo-cs-LC2" class="blob-code blob-code-inner js-file-line">public class MyTests</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-nunit_test_progress_todo-cs-LC3" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-nunit_test_progress_todo-cs-LC4" class="blob-code blob-code-inner js-file-line">    [Test]</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-nunit_test_progress_todo-cs-LC5" class="blob-code blob-code-inner js-file-line">    public void SampleTest()</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-nunit_test_progress_todo-cs-LC6" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-nunit_test_progress_todo-cs-LC7" class="blob-code blob-code-inner js-file-line">        // some operations here, like starting a server for tests in-memory&#8230;</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-nunit_test_progress_todo-cs-LC8" class="blob-code blob-code-inner js-file-line">        </td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-nunit_test_progress_todo-cs-LC9" class="blob-code blob-code-inner js-file-line">        // TODO: inform the &#39;user&#39; (the one who runs the test) that the server is already running</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-nunit_test_progress_todo-cs-LC10" class="blob-code blob-code-inner js-file-line">        while (true)</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-nunit_test_progress_todo-cs-LC11" class="blob-code blob-code-inner js-file-line">        {</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-nunit_test_progress_todo-cs-LC12" class="blob-code blob-code-inner js-file-line">            // keep it running on purpose (e.g. for E2E tests)</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-nunit_test_progress_todo-cs-LC13" class="blob-code blob-code-inner js-file-line">        }</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-nunit_test_progress_todo-cs-LC14" class="blob-code blob-code-inner js-file-line">        </td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-nunit_test_progress_todo-cs-LC15" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress_todo-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-nunit_test_progress_todo-cs-LC16" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/419247cf91eaa4bd67f820a9af5e0dd9/raw/1f387ac42e1bf490b1531fc7808de21fe2fe6aff/NUnit_test_progress_todo.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/419247cf91eaa4bd67f820a9af5e0dd9#file-nunit_test_progress_todo-cs" class="Link--inTextBlock">
          NUnit_test_progress_todo.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p class="wp-block-paragraph">The title picture of this article shows the end result. If you want to know the solution, keep reading <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4070"></span>



<h2 class="wp-block-heading">Context</h2>



<p class="wp-block-paragraph">The context of this need is very simple. I have a unit test in which I want to perform some operations and keep the test running indefinitely afterwards. The use case might be instantiation of an <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.testserver?view=aspnetcore-6.0">in-memory test server</a>. After that, I want to inform the user (programmer or the one running the test) that the server is up and running. I want to do that <strong>before the test finishes</strong>. In other words: <strong>I want to write the test&#8217;s output while it&#8217;s still running</strong>. </p>



<p class="wp-block-paragraph">In my case, I was running two .NET Core apps in-memory. The backend server app and the ASP.NET Core web application. In the process, the web app got its IP address assigned dynamically. Meaning that the IP was different with each test run. This particular test was used to run the app in-memory and let the user manually use the application. It means the user must know the IP/URL of the in-memory web app. That&#8217;s why I needed to output the dynamic IP address into the NUnit test output <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Solution</h2>



<p class="wp-block-paragraph">The solution to write test progress to the console with NUnit is very simple. To do that, use <code>TestContext.Progress.WriteLine(string)</code> method from <code>NUnit.Framework</code> namespace:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist116902272" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-nunit_test_progress-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="NUnit_test_progress.cs content, created by dsibinski on 05:05AM on June 19, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="NUnit_test_progress.cs">
        <tr>
          <td id="file-nunit_test_progress-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-nunit_test_progress-cs-LC1" class="blob-code blob-code-inner js-file-line">[TestFixture]</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-nunit_test_progress-cs-LC2" class="blob-code blob-code-inner js-file-line">public class MyTests</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-nunit_test_progress-cs-LC3" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-nunit_test_progress-cs-LC4" class="blob-code blob-code-inner js-file-line">    [Test]</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-nunit_test_progress-cs-LC5" class="blob-code blob-code-inner js-file-line">    public void SampleTest()</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-nunit_test_progress-cs-LC6" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-nunit_test_progress-cs-LC7" class="blob-code blob-code-inner js-file-line">        // some operations here, like starting a server for tests in-memory&#8230;</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-nunit_test_progress-cs-LC8" class="blob-code blob-code-inner js-file-line">        </td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-nunit_test_progress-cs-LC9" class="blob-code blob-code-inner js-file-line">        TestContext.Progress.WriteLine(&quot;The server is running now! You can reach it on https://localhost:8067/&quot;);</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-nunit_test_progress-cs-LC10" class="blob-code blob-code-inner js-file-line">        while (true)</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-nunit_test_progress-cs-LC11" class="blob-code blob-code-inner js-file-line">        {</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-nunit_test_progress-cs-LC12" class="blob-code blob-code-inner js-file-line">            // keep it running on purpose (e.g. for E2E tests)</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-nunit_test_progress-cs-LC13" class="blob-code blob-code-inner js-file-line">        }</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-nunit_test_progress-cs-LC14" class="blob-code blob-code-inner js-file-line">        </td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-nunit_test_progress-cs-LC15" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-nunit_test_progress-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-nunit_test_progress-cs-LC16" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/03221c21614c48cef6fdbbd5e11accb4/raw/1880d0e1f47cb145c600d96e5f1d392bf5728028/NUnit_test_progress.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/03221c21614c48cef6fdbbd5e11accb4#file-nunit_test_progress-cs" class="Link--inTextBlock">
          NUnit_test_progress.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p class="wp-block-paragraph">And that&#8217;s it! I&#8217;m publishing this simple solution, because it took me some time to find <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p class="wp-block-paragraph">I previously tried with <code>TestContext.Out.WriteLine</code>, <code>System.Diagnostics.Trace.WriteLine</code> and <code>System.Console.WriteLine</code>, but all of them output the text <em>after </em>the test finishes.</p>



<p class="wp-block-paragraph">Finally, you can also run the test from cmd using <code>dotnet test</code>:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="1024" height="189" data-attachment-id="4081" data-permalink="https://www.codejourney.net/write-test-progress-to-the-console-with-nunit/nunit_cmd_console_progress/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/06/NUnit_cmd_console_progress.png?fit=1119%2C206&amp;ssl=1" data-orig-size="1119,206" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="NUnit_cmd_console_progress" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/06/NUnit_cmd_console_progress.png?fit=1024%2C189&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/06/NUnit_cmd_console_progress.png?resize=1024%2C189&#038;ssl=1" alt="" class="wp-image-4081" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/06/NUnit_cmd_console_progress.png?resize=1024%2C189&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/06/NUnit_cmd_console_progress.png?resize=300%2C55&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/06/NUnit_cmd_console_progress.png?resize=768%2C141&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/06/NUnit_cmd_console_progress.png?w=1119&amp;ssl=1 1119w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p class="wp-block-paragraph">Hope it&#8217;s useful!</p>
<p>The post <a href="https://www.codejourney.net/write-test-progress-to-the-console-with-nunit/">Write Test Progress To The Console With NUnit</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/write-test-progress-to-the-console-with-nunit/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4070</post-id>	</item>
		<item>
		<title>Improve your tests with Assert Object Pattern</title>
		<link>https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/</link>
					<comments>https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 09 Nov 2020 20:42:34 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[assert object]]></category>
		<category><![CDATA[assert object pattern]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit tests]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3888</guid>

					<description><![CDATA[<p>The Asserts Hell Let&#8217;s consider the following unit test: What&#8217;s wrong here? Given and When sections are great single-liners. We know straightaway what&#8217;s the input and the action executed. However, Then block is too complex. It&#8217;s hard to figure out, just passing quickly through this test, what is expected. This case is even not that&#8230;</p>
<p>The post <a href="https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/">Improve your tests with Assert Object Pattern</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Today I&#8217;d like to share with you a very interesting concept in software testing &#8211; Assert Object pattern. It makes the <em>Assert</em> part of a test much simpler and more readable. Let&#8217;s dive right into it <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-3888"></span>





<h2 class="wp-block-heading">The Asserts Hell</h2>



<p class="wp-block-paragraph">Let&#8217;s consider the following unit test:</p>



<script src="https://gist.github.com/dsibinski/f8fea58079d68dd8ac0d194a33964e1b.js"></script>



<p class="wp-block-paragraph">What&#8217;s wrong here? <em>Given</em> and <em>When</em> sections are great single-liners. We know straightaway what&#8217;s the input and the action executed. However, <em>Then</em> block is too complex. It&#8217;s hard to figure out, just passing quickly through this test, what is <em>expected</em>. This case is even not that bad thanks to the usage of <a href="https://fluentassertions.com/">FluentAssertions</a>.</p>



<p class="wp-block-paragraph">I spend a lot of time writing tests (<a href="https://www.codejourney.net/2017/03/unit-testing-xamarin-application/">unit</a> or integration), but I spend even more time reading them. I always hope that I will find these <em>idyllic</em> tests that act as code documentation&#8230; <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9d0.png" alt="🧐" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p class="wp-block-paragraph">However, let&#8217;s go step by step. I recently took place in a <a href="https://smarttesting.pl/">software testing course</a>. That&#8217;s where I discovered the solution to <em>The Asserts Hell</em>.</p>



<h2 class="wp-block-heading">Assert Object for Better Asserts</h2>



<p class="wp-block-paragraph">Assert Object pattern is what solves our problem. The idea is to create an <em>Assert</em> class which wraps the original object being tested. In our case the tested object is of <span style="color:#ff9f05" class="tadv-color">Product</span> class, so our assert class will be called <span style="color:#ff9f05" class="tadv-color">ProductAssert</span>.</p>



<p class="wp-block-paragraph">Let&#8217;s see the implementation:</p>



<script src="https://gist.github.com/dsibinski/10a3c47a7e997eed29fabcb07914786b.js"></script>



<p class="wp-block-paragraph">As you can see, we simply moved the asserts from our test&#8217;s Then section into <span style="color:#ff9f05" class="tadv-color">ProductAssert</span> class&#8217;s methods. Additionally, we always return <span style="color:#ff9f05" class="tadv-color">this</span> from each asserting method, which allows chaining functions calls.</p>



<p class="wp-block-paragraph">We can now use such assert object<span style="color:#444" class="tadv-color"> in our</span> unit test: </p>



<script src="https://gist.github.com/dsibinski/e0a649e6134b9c6b389d3419db62f92a.js"></script>



<p class="wp-block-paragraph">You must agree it&#8217;s much more readable now <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p class="wp-block-paragraph">We can of course improve it even more, for example by wrapping these 3 assertion methods (<span style="color:#ff9f05" class="tadv-color">BeAvailable()</span>, <span style="color:#ff9f05" class="tadv-color">HaveDiscount() </span><span style="color:#444" class="tadv-color">and</span><span style="color:#ff9f05" class="tadv-color"> Cost()</span>) into a single one. We can also easily create an <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods">extension method</a> for the <span style="color:#ff9f05" class="tadv-color">Product</span> class itself, so the assert object doesn&#8217;t have to be instantiated in our unit test. I could even add an extension to <em>FluentAssertions</em> library.</p>



<p class="wp-block-paragraph">You can find complete source code used as examples in this article <a href="https://github.com/dsibinski/Playground/tree/main/Playground/Playground.Tests">here</a>.</p>



<h2 class="wp-block-heading">Assert Object &#8211; summary</h2>



<p class="wp-block-paragraph">I personally find Assert Object a very useful tests code refactoring method. As a programmer, you spend most of your time not on writing, but on reading the source code. Clear and simple tests can make your (and your colleagues&#8217;) life much better <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p class="wp-block-paragraph">Of course, the Assert Object pattern should not be your default way of asserting. If you can complete your test with one or two assertions, then it&#8217;s probably better to keep it without wrapping into any additional objects. Having too many <em>assert</em> statements might also mean that the objects being tested are poorly designed. However, as we often work with legacy code which we can&#8217;t easily change or refactor, it might be a good solution.</p>



<p class="wp-block-paragraph">What patterns for writing better tests do you use? Share your tips in the comments!</p>
<p>The post <a href="https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/">Improve your tests with Assert Object Pattern</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3888</post-id>	</item>
		<item>
		<title>Unit Testing Xamarin application</title>
		<link>https://www.codejourney.net/unit-testing-xamarin-application/</link>
					<comments>https://www.codejourney.net/unit-testing-xamarin-application/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sat, 25 Mar 2017 23:07:19 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dajsiepoznac2017]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[xamarin]]></category>
		<guid isPermaLink="false">http://www.dsibinski.pl/?p=1166</guid>

					<description><![CDATA[<p>Today we&#8217;ll see how to add unit tests to Xamarin Android application, testing both platform-independent logic and Android-specific features. Issue with unit tests in Visual Studio 2017 When I started to create my data access methods in MoneyBack, I really wanted to start writing even some basic unit tests of that logic. My first trial was to&#8230;</p>
<p>The post <a href="https://www.codejourney.net/unit-testing-xamarin-application/">Unit Testing Xamarin application</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Today we&#8217;ll see how to add unit tests to Xamarin Android application, testing both platform-independent logic and Android-specific features.<br />
<span id="more-1166"></span></p>
<h2>Issue with unit tests in Visual Studio 2017</h2>
<p>When I started to create my data access methods in <em>MoneyBack</em>, I really wanted to start writing even some basic unit tests of that logic.</p>
<p>My first trial was to add a new project to my solution which contains unit tests. So I checked what kind of projects templates I have available and I I found <strong>Unit Test App (Android) </strong>project template and added it to my solution. Generated project contained <span style="color: #ff6600;"><em>TestsSample</em> </span>class with some unit-looking tests methods, but I had no idea how to execute them. With ReSharper installed, I didn&#8217;t have any &#8220;Run Tests&#8221; option on this project. I read somewhere on the web that this project type is used to execute tests on the device, but I couldn&#8217;t figure out how to do it. I gave up.</p>
<p>Then I found another project template &#8211; <strong>Class Library (Android) with nUnit</strong>. Sounds better, <em>nUnit</em> in the name suggests unit tests, so I added this project to my solution. This time when right-clicking the project I had &#8220;Run Tests&#8221; option, but when clicking on it I got the following exception coming from ReSharper:</p>
<p><figure id="attachment_1200" aria-describedby="caption-attachment-1200" style="width: 1030px" class="wp-caption aligncenter"><img data-recalc-dims="1" decoding="async" data-attachment-id="1200" data-permalink="https://www.codejourney.net/unit-testing-xamarin-application/resharper_exc/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?fit=1030%2C829&amp;ssl=1" data-orig-size="1030,829" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="ReSharper_Exc" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?fit=1024%2C824&amp;ssl=1" class="wp-image-1200 size-full" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?resize=1030%2C829&#038;ssl=1" alt="ReSharper_Exc" width="1030" height="829" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?w=1030&amp;ssl=1 1030w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?resize=300%2C241&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?resize=768%2C618&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?resize=1024%2C824&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/ReSharper_Exc.png?resize=720%2C579&amp;ssl=1 720w" sizes="(max-width: 1030px) 100vw, 1030px" /><figcaption id="caption-attachment-1200" class="wp-caption-text">ReSharper error when executing unit tests</figcaption></figure></p>
<p>&#8220;Hmm&#8230; Visual Studio 2017&#8221;, I thought. &#8220;Some bugs for sure&#8221;. And I&#8230;</p>
<p>&#8230; didn&#8217;t give up this time and with some help of folks from StackOverflow I figured out how unit tests should be done in Xamarin app. Let&#8217;s see that in the next chapters.</p>
<h2>Levels of testing Xamarin application</h2>
<p>There are basically three &#8220;levels&#8221; of testing Xamarin apps:</p>
<ol>
<li><strong>Classic unit tests of pure .NET/Mono</strong>
<ul>
<li>standard, good old unit tests of logic independent from targeting platform (Xamarin.Android/iOS/Windows Phone)</li>
<li>unit-testing frameworks can be used (nUnit / xUnit)</li>
</ul>
</li>
<li><strong>Platform-specific tests</strong>
<ul>
<li>unit tests of functionalities specific to targeting platform (e.g. Bluetooth, Location, GPS, SMS etc.)</li>
<li>specific for each platform</li>
<li>don&#8217;t contain GUI tests</li>
<li>executed on the emulator/device</li>
</ul>
</li>
<li><strong>UI tests</strong>
<ul>
<li>tests of UI elements of the app and how those react for input (touch) events</li>
<li>executed by cloud/testing lab services (local or remote)</li>
</ul>
</li>
</ol>
<p>We&#8217;ll dive into the first two, UI tests is more complex topic, which requires setting up tests lab or using cloud testing services. For small projects, in the beginning it&#8217;s not necessary.</p>
<h2>Classic platform-independent unit tests</h2>
<p>In order to simply use ReSharper for executing unit tests of logic, which is independent from Android/iOS/Windows Phone, there is a need to add a new project using template <strong>Unit Test Project (.NET Framework) </strong>to our Xamarin solution.</p>
<p>To such project, you can add your favourite unit-testing framework (such as <em>nUnit</em> or <em>xUnit</em>) by simply installing it from Nuget.  After, ReSharper allows to run your unit tests as in old good times:</p>
<p><figure id="attachment_1246" aria-describedby="caption-attachment-1246" style="width: 714px" class="wp-caption aligncenter"><img data-recalc-dims="1" decoding="async" data-attachment-id="1246" data-permalink="https://www.codejourney.net/unit-testing-xamarin-application/tests_rsharper/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/tests_rsharper.png?fit=714%2C149&amp;ssl=1" data-orig-size="714,149" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="tests_rsharper" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/tests_rsharper.png?fit=714%2C149&amp;ssl=1" class="wp-image-1246 size-full" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/tests_rsharper.png?resize=714%2C149&#038;ssl=1" alt="tests_rsharper.PNG" width="714" height="149" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/tests_rsharper.png?w=714&amp;ssl=1 714w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/tests_rsharper.png?resize=300%2C63&amp;ssl=1 300w" sizes="(max-width: 714px) 100vw, 714px" /><figcaption id="caption-attachment-1246" class="wp-caption-text">nUnit tests executed by ReSharper</figcaption></figure></p>
<p>The exception I was getting with ReSharper previously was because I added the project using template <em>Class Library (Android) </em>which has its target set to Android device/emulator. ReSharper doesn&#8217;t know about any Android-specific (or Xamarin-specific) testing, so it was displaying unhandled error.</p>
<h2>Platform-specific unit tests</h2>
<p>In order to test platform-specific (in out case Android-specific) functionalities of your application, the tests need targeting runtime environment to run. It means they need to be executed on the physical device or emulator. Providers of the most popular unit-testing frameworks created wrappers for on-device testing. I&#8217;m using <a href="https://github.com/nunit/nunit.xamarin" target="_blank" rel="noopener">NUnit Xamarin Runners</a>.</p>
<p>First of all, a new project should be added to our solution. To make things easier, download <a href="https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTemplatesforVisualStudio" target="_blank" rel="noopener">NUnit Templates for Visual Studio</a> (although it officially supports VS up to 2015, it also works in VS 2017) and install it. After, add new project using appropriate template &#8211; I chose <strong>NUnit 3 Test Project (Android)</strong>. What&#8217;s interesting, the newly created project contains <em><span style="color: #ff6600;">MainActivity</span> </em>class with the following content:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist46061652" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-mainactivity_tests-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="MainActivity_Tests.cs content, created by dsibinski on 10:13PM on March 25, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="MainActivity_Tests.cs">
<tr>
<td id="file-mainactivity_tests-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-mainactivity_tests-cs-LC1" class="blob-code blob-code-inner js-file-line">  public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-mainactivity_tests-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-mainactivity_tests-cs-LC3" class="blob-code blob-code-inner js-file-line">      protected override void OnCreate(Bundle savedInstanceState)</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-mainactivity_tests-cs-LC4" class="blob-code blob-code-inner js-file-line">      {</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-mainactivity_tests-cs-LC5" class="blob-code blob-code-inner js-file-line">          base.OnCreate(savedInstanceState);</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-mainactivity_tests-cs-LC6" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-mainactivity_tests-cs-LC7" class="blob-code blob-code-inner js-file-line">          global::Xamarin.Forms.Forms.Init(this, savedInstanceState);</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-mainactivity_tests-cs-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-mainactivity_tests-cs-LC9" class="blob-code blob-code-inner js-file-line">          // This will load all tests within the current project</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-mainactivity_tests-cs-LC10" class="blob-code blob-code-inner js-file-line">          var nunit = new NUnit.Runner.App();</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-mainactivity_tests-cs-LC11" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-mainactivity_tests-cs-LC12" class="blob-code blob-code-inner js-file-line">          // If you want to add tests in another assembly</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-mainactivity_tests-cs-LC13" class="blob-code blob-code-inner js-file-line">          //nunit.AddTestAssembly(typeof(MyTests).Assembly);</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-mainactivity_tests-cs-LC14" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-mainactivity_tests-cs-LC15" class="blob-code blob-code-inner js-file-line">          // Do you want to automatically run tests when the app starts?</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-mainactivity_tests-cs-LC16" class="blob-code blob-code-inner js-file-line">          nunit.AutoRun = true;</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-mainactivity_tests-cs-LC17" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-mainactivity_tests-cs-LC18" class="blob-code blob-code-inner js-file-line">          LoadApplication(nunit);</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-mainactivity_tests-cs-LC19" class="blob-code blob-code-inner js-file-line">      }</td>
</tr>
<tr>
<td id="file-mainactivity_tests-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
<td id="file-mainactivity_tests-cs-LC20" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/ea386a734c1021a4ba5430e0306ab732/raw/fc5a31fd21ae36379882b0993be91d216944c4ec/MainActivity_Tests.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/ea386a734c1021a4ba5430e0306ab732#file-mainactivity_tests-cs" class="Link--inTextBlock"><br />
          MainActivity_Tests.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>As you can see, this is an <a href="https://www.codejourney.net/2017/03/xamarin-android-activities/" target="_blank" rel="noopener">Activity</a>, so it will be launched as a separate app with its own GUI. It&#8217;s built using Xamarin.Forms. The comments in auto-generated class are obvious, so I won&#8217;t explain the details here. That&#8217;s just what nUnit wrapper for Android on-device tests does &#8211; it allows to be run as a separate, small application with a GUI allowing to run unit tests directly on the device (similarly to ReSharper tests runner we use directly in Visual Studio).</p>
<p>One testable piece of <em>MoneyBack</em> application is the <em>Repository</em> used for database operations (by the way I refactored and made it generic, you can see how it looks currently on <a href="https://github.com/dsibinski/MoneyBack" target="_blank" rel="noopener">GitHub</a>), so I added <span style="color: #ff6600;"><em>RepositoryTests</em></span> class to <em>MoneyBack.Android.Tests</em> project with two test methods:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist46061804" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-repositorytests_methods-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="RepositoryTests_methods.cs content, created by dsibinski on 10:22PM on March 25, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="RepositoryTests_methods.cs">
<tr>
<td id="file-repositorytests_methods-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-repositorytests_methods-cs-LC1" class="blob-code blob-code-inner js-file-line">[Test]</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-repositorytests_methods-cs-LC2" class="blob-code blob-code-inner js-file-line">public void one_new_person_inserted_adds_one_new_row()</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-repositorytests_methods-cs-LC3" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-repositorytests_methods-cs-LC4" class="blob-code blob-code-inner js-file-line">    // given</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-repositorytests_methods-cs-LC5" class="blob-code blob-code-inner js-file-line">    var person = new Person()</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-repositorytests_methods-cs-LC6" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-repositorytests_methods-cs-LC7" class="blob-code blob-code-inner js-file-line">        Name = &quot;A&quot;,</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-repositorytests_methods-cs-LC8" class="blob-code blob-code-inner js-file-line">        LastName = &quot;B&quot;</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-repositorytests_methods-cs-LC9" class="blob-code blob-code-inner js-file-line">    };</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-repositorytests_methods-cs-LC10" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-repositorytests_methods-cs-LC11" class="blob-code blob-code-inner js-file-line">    var repo = new Repository&lt;Person&gt;(InMemorySqliteConnection);</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-repositorytests_methods-cs-LC12" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-repositorytests_methods-cs-LC13" class="blob-code blob-code-inner js-file-line">    // when</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-repositorytests_methods-cs-LC14" class="blob-code blob-code-inner js-file-line">    var numRows = repo.Insert(person).Result;</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-repositorytests_methods-cs-LC15" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-repositorytests_methods-cs-LC16" class="blob-code blob-code-inner js-file-line">    // then</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-repositorytests_methods-cs-LC17" class="blob-code blob-code-inner js-file-line">    Assert.AreEqual(1, numRows);</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-repositorytests_methods-cs-LC18" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-repositorytests_methods-cs-LC19" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
<td id="file-repositorytests_methods-cs-LC20" class="blob-code blob-code-inner js-file-line">[Test]</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
<td id="file-repositorytests_methods-cs-LC21" class="blob-code blob-code-inner js-file-line">public void new_person_added_has_id_primarykey_generated()</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
<td id="file-repositorytests_methods-cs-LC22" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
<td id="file-repositorytests_methods-cs-LC23" class="blob-code blob-code-inner js-file-line">    // given</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
<td id="file-repositorytests_methods-cs-LC24" class="blob-code blob-code-inner js-file-line">    var person1 = new Person</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
<td id="file-repositorytests_methods-cs-LC25" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
<td id="file-repositorytests_methods-cs-LC26" class="blob-code blob-code-inner js-file-line">        Name = &quot;A&quot;,</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
<td id="file-repositorytests_methods-cs-LC27" class="blob-code blob-code-inner js-file-line">        LastName = &quot;B&quot;</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
<td id="file-repositorytests_methods-cs-LC28" class="blob-code blob-code-inner js-file-line">    };</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L29" class="blob-num js-line-number js-blob-rnum" data-line-number="29"></td>
<td id="file-repositorytests_methods-cs-LC29" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L30" class="blob-num js-line-number js-blob-rnum" data-line-number="30"></td>
<td id="file-repositorytests_methods-cs-LC30" class="blob-code blob-code-inner js-file-line">    var person2 = new Person</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L31" class="blob-num js-line-number js-blob-rnum" data-line-number="31"></td>
<td id="file-repositorytests_methods-cs-LC31" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L32" class="blob-num js-line-number js-blob-rnum" data-line-number="32"></td>
<td id="file-repositorytests_methods-cs-LC32" class="blob-code blob-code-inner js-file-line">        Name = &quot;A&quot;,</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L33" class="blob-num js-line-number js-blob-rnum" data-line-number="33"></td>
<td id="file-repositorytests_methods-cs-LC33" class="blob-code blob-code-inner js-file-line">        LastName = &quot;B&quot;</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L34" class="blob-num js-line-number js-blob-rnum" data-line-number="34"></td>
<td id="file-repositorytests_methods-cs-LC34" class="blob-code blob-code-inner js-file-line">    };</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L35" class="blob-num js-line-number js-blob-rnum" data-line-number="35"></td>
<td id="file-repositorytests_methods-cs-LC35" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L36" class="blob-num js-line-number js-blob-rnum" data-line-number="36"></td>
<td id="file-repositorytests_methods-cs-LC36" class="blob-code blob-code-inner js-file-line">    var repo = new Repository&lt;Person&gt;(InMemorySqliteConnection);</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L37" class="blob-num js-line-number js-blob-rnum" data-line-number="37"></td>
<td id="file-repositorytests_methods-cs-LC37" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L38" class="blob-num js-line-number js-blob-rnum" data-line-number="38"></td>
<td id="file-repositorytests_methods-cs-LC38" class="blob-code blob-code-inner js-file-line">    // when</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L39" class="blob-num js-line-number js-blob-rnum" data-line-number="39"></td>
<td id="file-repositorytests_methods-cs-LC39" class="blob-code blob-code-inner js-file-line">    var n1 = repo.Insert(person1).Result; // getting Result in order to force Task&#39;s completion before continuing</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L40" class="blob-num js-line-number js-blob-rnum" data-line-number="40"></td>
<td id="file-repositorytests_methods-cs-LC40" class="blob-code blob-code-inner js-file-line">    var n2 = repo.Insert(person2).Result;</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L41" class="blob-num js-line-number js-blob-rnum" data-line-number="41"></td>
<td id="file-repositorytests_methods-cs-LC41" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L42" class="blob-num js-line-number js-blob-rnum" data-line-number="42"></td>
<td id="file-repositorytests_methods-cs-LC42" class="blob-code blob-code-inner js-file-line">    // then</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L43" class="blob-num js-line-number js-blob-rnum" data-line-number="43"></td>
<td id="file-repositorytests_methods-cs-LC43" class="blob-code blob-code-inner js-file-line">    Assert.Greater(person1.Id, 0);</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L44" class="blob-num js-line-number js-blob-rnum" data-line-number="44"></td>
<td id="file-repositorytests_methods-cs-LC44" class="blob-code blob-code-inner js-file-line">    Assert.AreEqual(person2.Id, person1.Id + 1);</td>
</tr>
<tr>
<td id="file-repositorytests_methods-cs-L45" class="blob-num js-line-number js-blob-rnum" data-line-number="45"></td>
<td id="file-repositorytests_methods-cs-LC45" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/2cefa097384d3c5e33595f37392cc330/raw/444e616d3480cdad0bda4c96e7112c7e65b2f698/RepositoryTests_methods.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/2cefa097384d3c5e33595f37392cc330#file-repositorytests_methods-cs" class="Link--inTextBlock"><br />
          RepositoryTests_methods.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>The interesting part is that as I&#8217;m using <a href="https://www.codejourney.net/2017/03/using-sqlite-database-in-xamarin-android/" target="_blank" rel="noopener">SQLite database in my app</a>, I wanted to test it without creating database file on the device. For that purpose, when creating <em><span style="color: #ff6600;">SQLiteAsyncConnection</span> </em>in order<em> </em>to initialize my <em><span style="color: #ff6600;">Repository</span>, </em>I passed to its constructor a <em>&#8220;:memory:&#8221;</em> string, which makes SQLite database created <a href="https://sqlite.org/inmemorydb.html" target="_blank" rel="noopener">in-memory</a> (there is no physical file created):</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist46061888" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-repositorytests_sqlite-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="RepositoryTests_sqlite.cs content, created by dsibinski on 10:27PM on March 25, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="RepositoryTests_sqlite.cs">
<tr>
<td id="file-repositorytests_sqlite-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-repositorytests_sqlite-cs-LC1" class="blob-code blob-code-inner js-file-line">  public SQLiteAsyncConnection InMemorySqliteConnection;</td>
</tr>
<tr>
<td id="file-repositorytests_sqlite-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-repositorytests_sqlite-cs-LC2" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-repositorytests_sqlite-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-repositorytests_sqlite-cs-LC3" class="blob-code blob-code-inner js-file-line">  [OneTimeSetUp]</td>
</tr>
<tr>
<td id="file-repositorytests_sqlite-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-repositorytests_sqlite-cs-LC4" class="blob-code blob-code-inner js-file-line">  public void Init()</td>
</tr>
<tr>
<td id="file-repositorytests_sqlite-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-repositorytests_sqlite-cs-LC5" class="blob-code blob-code-inner js-file-line">  {</td>
</tr>
<tr>
<td id="file-repositorytests_sqlite-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-repositorytests_sqlite-cs-LC6" class="blob-code blob-code-inner js-file-line">      InMemorySqliteConnection = new SQLiteAsyncConnection(&quot;:memory:&quot;);</td>
</tr>
<tr>
<td id="file-repositorytests_sqlite-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-repositorytests_sqlite-cs-LC7" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/e205d0eacd107226c0986a2b9d2d8422/raw/53f9fb2a2be3c5f28320fbc09049b087694ed624/RepositoryTests_sqlite.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/e205d0eacd107226c0986a2b9d2d8422#file-repositorytests_sqlite-cs" class="Link--inTextBlock"><br />
          RepositoryTests_sqlite.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>&nbsp;</p>
<p>Such database lives in device&#8217;s memory only for the time of our unit tests.</p>
<p>After having tests written, there&#8217;s a time to execute them. As the project contains <em><span style="color: #ff6600;">MainActivity</span> </em>class, we simply deploy and run it on a device/emulator, which presents the following view:</p>
<p><figure id="attachment_1306" aria-describedby="caption-attachment-1306" style="width: 210px" class="wp-caption aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="1306" data-permalink="https://www.codejourney.net/unit-testing-xamarin-application/screenshot_2017-03-25-23-31-44/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?fit=1080%2C1920&amp;ssl=1" data-orig-size="1080,1920" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Screenshot_2017-03-25-23-31-44" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?fit=576%2C1024&amp;ssl=1" class=" wp-image-1306 aligncenter" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?resize=210%2C374&#038;ssl=1" alt="Screenshot_2017-03-25-23-31-44.png" width="210" height="374" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?w=1080&amp;ssl=1 1080w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?resize=169%2C300&amp;ssl=1 169w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?resize=768%2C1365&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?resize=576%2C1024&amp;ssl=1 576w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-23-31-44.png?resize=720%2C1280&amp;ssl=1 720w" sizes="auto, (max-width: 210px) 100vw, 210px" /><figcaption id="caption-attachment-1306" class="wp-caption-text">Device &#8211; tests results</figcaption></figure></p>
<p>If any of our tests is not passed, we can see the failure&#8217;s details:</p>
<p><figure id="attachment_1311" aria-describedby="caption-attachment-1311" style="width: 219px" class="wp-caption aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="1311" data-permalink="https://www.codejourney.net/unit-testing-xamarin-application/screenshot_2017-03-25-21-23-07/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?fit=1080%2C1920&amp;ssl=1" data-orig-size="1080,1920" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Screenshot_2017-03-25-21-23-07" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?fit=576%2C1024&amp;ssl=1" class=" wp-image-1311 aligncenter" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?resize=219%2C389&#038;ssl=1" alt="Screenshot_2017-03-25-21-23-07.png" width="219" height="389" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?w=1080&amp;ssl=1 1080w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?resize=169%2C300&amp;ssl=1 169w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?resize=768%2C1365&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?resize=576%2C1024&amp;ssl=1 576w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/Screenshot_2017-03-25-21-23-07.png?resize=720%2C1280&amp;ssl=1 720w" sizes="auto, (max-width: 219px) 100vw, 219px" /><figcaption id="caption-attachment-1311" class="wp-caption-text">Device &#8211; test failed</figcaption></figure></p>
<p>We can of course debug our units tests in Visual Studio. The GUI on the device also allows to re-run the tests and see results of all of them.</p>
<h2>Summary</h2>
<p>We&#8217;ve seen how to add two new projects to Xamarin solution: one containing platform-independent unit tests (old good ones), the other one testing Android-specific stuff which is run directly on the device or emulator.</p>
<p>For bigger and more &#8220;real&#8221; apps, UI testing would also be necessary. This however requires using some cloud testing lab or setting up your own testing environment. More details can be found <a href="https://developer.xamarin.com/guides/testcloud/introduction-to-test-cloud/" target="_blank" rel="noopener">here</a>.</p>
<p><span style="text-decoration: underline;"><strong>If you have any experience in testing Xamarin applications, share your insights/advices <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></strong></span></p>
<h2> </h2>
<p>&nbsp;</p>
<p>The post <a href="https://www.codejourney.net/unit-testing-xamarin-application/">Unit Testing Xamarin application</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/unit-testing-xamarin-application/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1166</post-id>	</item>
	</channel>
</rss>
