What makes a clean test❓
Three things : Readability, readability, and readability.
Read-ability is perhaps even more important in unit tests than it is in production code .
What makes tests readable ❔
The same thing that makes all code readable :
clarity, simplicity & density of expression .
#CleanCode #UnitTest
Three things : Readability, readability, and readability.
Read-ability is perhaps even more important in unit tests than it is in production code .
What makes tests readable ❔
The same thing that makes all code readable :
clarity, simplicity & density of expression .
#CleanCode #UnitTest
What is Unit Test ❓
A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. ✅
If the assumptions turn out to be wrong, the unit test has failed. ❌
A unit is a method or function.
#UnitTest
@ProgrammingTip
A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. ✅
If the assumptions turn out to be wrong, the unit test has failed. ❌
A unit is a method or function.
#UnitTest
@ProgrammingTip
Properties of a good unit test ✅
A unit test should have the following properties :
1️⃣ It should be automated and repeatable.
2️⃣ It should be easy to implement.
3️⃣ It should be relevant tomorrow.
4️⃣ Anyone should be able to run it at the push of a button.
5️⃣ It should run quickly.
6️⃣ It should be consistent in its results.
7️⃣ It should have full control of the unit under test.
8️⃣ It should be fully isolated (runs independently of other tests).
9️⃣ When it fails, it should be easy to detect what was expected and determine how to pinpoint the problem.
#UnitTest #Tips
@ProgrammingTip
A unit test should have the following properties :
1️⃣ It should be automated and repeatable.
2️⃣ It should be easy to implement.
3️⃣ It should be relevant tomorrow.
4️⃣ Anyone should be able to run it at the push of a button.
5️⃣ It should run quickly.
6️⃣ It should be consistent in its results.
7️⃣ It should have full control of the unit under test.
8️⃣ It should be fully isolated (runs independently of other tests).
9️⃣ When it fails, it should be easy to detect what was expected and determine how to pinpoint the problem.
#UnitTest #Tips
@ProgrammingTip
Why use isolation frameworks❓
An isolation framework is a set of programmable APIs that makes creating fake objects much simpler, faster, and shorter than hand-coding them. 💨
#UnitTest #Mock
@ProgrammingTip
An isolation framework is a set of programmable APIs that makes creating fake objects much simpler, faster, and shorter than hand-coding them. 💨
#UnitTest #Mock
@ProgrammingTip
The Safe Green Zone ✅
Locate your integration and unit tests in separate places.
By doing that, you give the developers on your team a safe green test area that contains only unit tests, where they know that they can get the latest code version.
They can run all tests in that namespace or folder, and the tests should all be green.
If some tests in the safe green zone don’t pass ❌ ,
there’s a real problem, not a (false positive) configuration problem in the test.
#UnitTest
@ProgrammingTip
Locate your integration and unit tests in separate places.
By doing that, you give the developers on your team a safe green test area that contains only unit tests, where they know that they can get the latest code version.
They can run all tests in that namespace or folder, and the tests should all be green.
If some tests in the safe green zone don’t pass ❌ ,
there’s a real problem, not a (false positive) configuration problem in the test.
#UnitTest
@ProgrammingTip
Tests are part of source control ✅
The test code you write needs to reside in a source control repository, just like your real production code.📦
In fact, you should treat your test code as thoughtfully as you treat your production code. ↔️
It should be part of the branch for each version of the product, and it should be part of the code that developers receive automatically when they get the latest version. 🔚
Because unit tests are so connected to the code and API, they should always stay attached to the version of the code they’re testing.
#UnitTest
@ProgrammingTip
The test code you write needs to reside in a source control repository, just like your real production code.📦
In fact, you should treat your test code as thoughtfully as you treat your production code. ↔️
It should be part of the branch for each version of the product, and it should be part of the code that developers receive automatically when they get the latest version. 🔚
Because unit tests are so connected to the code and API, they should always stay attached to the version of the code they’re testing.
#UnitTest
@ProgrammingTip
Renaming or Refactoring Tests ♻️
An unreadable test is more of a problem than a solution. 👎🏿
It can hinder your code’s readability and your understanding of any problems it finds. ❌
If you encounter a test that has a vague or misleading name or that can be made more maintainable, change the test code. ✅
#UnitTest
@ProgrammingTip
An unreadable test is more of a problem than a solution. 👎🏿
It can hinder your code’s readability and your understanding of any problems it finds. ❌
If you encounter a test that has a vague or misleading name or that can be made more maintainable, change the test code. ✅
#UnitTest
@ProgrammingTip
Unit Tests ⚜️
Tests are stories you tell the next generation of programmers on a project. ✔️
They allow a developer to see exactly what an application is made of and where it started.
#UnitTest
@ProgrammingTip 💻
Tests are stories you tell the next generation of programmers on a project. ✔️
They allow a developer to see exactly what an application is made of and where it started.
#UnitTest
@ProgrammingTip 💻
GenFu 🤺
GenFu is a library you can use to generate realistic test data. ✅
It is composed of several property fillers that can populate commonly named properties through reflection using an internal database of values or randomly created data.
You can override any of the fillers, give GenFu hints on how to fill them. 🛠
[ Website ] : http://genfu.io/
https://t.me/pgimg/9
〰〰〰〰〰〰
#Dotnet #Core #UnitTest
@ProgrammingTip
GenFu is a library you can use to generate realistic test data. ✅
It is composed of several property fillers that can populate commonly named properties through reflection using an internal database of values or randomly created data.
You can override any of the fillers, give GenFu hints on how to fill them. 🛠
[ Website ] : http://genfu.io/
https://t.me/pgimg/9
〰〰〰〰〰〰
#Dotnet #Core #UnitTest
@ProgrammingTip
Telegram
Programming Tips Resources
What is Unit Testing & Why You Need to Lean It ⁉️
Unit testing is the practice of writing code to test your code and then run those tests in an automated fashion. ✅
🔹🔸🔹🔸
Here is an example. 👨🏻💻
Imagine you have this function somewhere in your code.
It’s a basic calculate function that takes an input and depending on some conditions, it returns different values.
If you want to test this function manually :
1️⃣ You have to run your application
2️⃣ Perhaps you have to login
3️⃣ Maybe do a few clicks here
4️⃣ There to get to a page where this function is used.
5️⃣ You have to fill out a form
6️⃣ Submit it
7️⃣ Verify if this function returned the right result.
And then you have to repeat all these steps, each time using different values in your form. 🤦🏻♂️
🔸🔹🔸🔹
Manual testing is expensive 💸
As you can see, this is very time-consuming. ⌛️
This workflow to test this function may take several minutes every time❗️
Now to make matters worse, this is not the only function in your application. 🌍
In a real application, you have tens or hundreds of functions like this ❗️
As your application grows in size and complexity, the time required to manually test all the different bits and pieces increases exponentially. ⏱
So, that’s why we use Automated Testing. ♻️
🔺🔹🔺🔹
https://t.me/pgimg/24
[ Full Article ] : http://bit.do/utdd
〰〰〰〰〰〰
#CleanCode #UnitTest #TDD
@ProgrammingTip
Unit testing is the practice of writing code to test your code and then run those tests in an automated fashion. ✅
🔹🔸🔹🔸
Here is an example. 👨🏻💻
Imagine you have this function somewhere in your code.
It’s a basic calculate function that takes an input and depending on some conditions, it returns different values.
public float CalculateTax(int input)
{
if (x) return ...;
if (y) return ...;
return ...;
}
If you want to test this function manually :
1️⃣ You have to run your application
2️⃣ Perhaps you have to login
3️⃣ Maybe do a few clicks here
4️⃣ There to get to a page where this function is used.
5️⃣ You have to fill out a form
6️⃣ Submit it
7️⃣ Verify if this function returned the right result.
And then you have to repeat all these steps, each time using different values in your form. 🤦🏻♂️
🔸🔹🔸🔹
Manual testing is expensive 💸
As you can see, this is very time-consuming. ⌛️
This workflow to test this function may take several minutes every time❗️
Now to make matters worse, this is not the only function in your application. 🌍
In a real application, you have tens or hundreds of functions like this ❗️
As your application grows in size and complexity, the time required to manually test all the different bits and pieces increases exponentially. ⏱
So, that’s why we use Automated Testing. ♻️
🔺🔹🔺🔹
https://t.me/pgimg/24
[ Full Article ] : http://bit.do/utdd
〰〰〰〰〰〰
#CleanCode #UnitTest #TDD
@ProgrammingTip
Telegram
Programming Tips Resources
Mocking objects with Moq and XUnit in .NET Core 👾
This article explains the step by step process that needed to Moq an object in .NET Core ⚜️
Mocking objects comes in handy when unit testing data store, where a Database can be mocked so that no data is added or modified in Database while unit testing source code. ♻️
https://t.me/pgimg/32
[ Tutorial ] : http://bit.do/moqx
〰〰〰〰〰〰
#UnitTest #Mocking #XUnit #Moq
@ProgrammingTip
This article explains the step by step process that needed to Moq an object in .NET Core ⚜️
Mocking objects comes in handy when unit testing data store, where a Database can be mocked so that no data is added or modified in Database while unit testing source code. ♻️
https://t.me/pgimg/32
[ Tutorial ] : http://bit.do/moqx
〰〰〰〰〰〰
#UnitTest #Mocking #XUnit #Moq
@ProgrammingTip
Telegram
Programming Tips Resources
Write tests,
Not too many,
Mostly integration. ✅
A while back, Guillermo Rauch ( Creator of Socket.io ) tweeted something profound :
This is deep, albeit short, so let’s dive in. 🌀
https://t.me/pgimg/62
[ Article ] : http://bit.do/iut
〰〰〰〰〰〰
#UnitTest
@ProgrammingTip
Not too many,
Mostly integration. ✅
A while back, Guillermo Rauch ( Creator of Socket.io ) tweeted something profound :
Write tests. Not too many. Mostly integration.💎
This is deep, albeit short, so let’s dive in. 🌀
https://t.me/pgimg/62
[ Article ] : http://bit.do/iut
〰〰〰〰〰〰
#UnitTest
@ProgrammingTip
Telegram
Programming Tips Resources
WireMock.NET 😮
WireMock.NET is a .NET library for stubbing and mocking HTTP services. With WireMock.NET, you can define the expected responses for particular requests, and the library will intercept and manage those requests for you.✔️
This allows for easy testing of the code that makes HTTP requests, without having to rely on the actual external service being available and without hacking HttpClient.🆒
Sample code snippet:
〰️〰️〰️〰️〰️〰️
#UnitTest #DotNet #CSharp
@ProgrammingTip
WireMock.NET is a .NET library for stubbing and mocking HTTP services. With WireMock.NET, you can define the expected responses for particular requests, and the library will intercept and manage those requests for you.
This allows for easy testing of the code that makes HTTP requests, without having to rely on the actual external service being available and without hacking HttpClient.
Sample code snippet:
[Test][ Blog ] : https://cezarypiatek.github.io/post/mocking-outgoing-http-requests-p1
public async Task sample_WireMock_usage()
{
// Setup WireMock.Net server
using var wireMock = WireMockServer.StartWithAdminInterface(port: 1080, ssl: false);
// Setup WebApplicationFactory
await using var appFactory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration(configurationBuilder =>
{
// Override downstream service addresses pointing to WireMock address
configurationBuilder.AddInMemoryCollection(new Dictionary<string, string>
{
["ExternalServices:WeatherService"] = "http://localhost:1080"
});
});
});
// Prepare stub for outgoing request
wireMock
.Given(
Request.Create()
.WithPath("/api/v1.0/weather")
.WithParam("lat", "10.99")
.WithParam("lon", "44.34")
.UsingGet()
)
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json; charset=utf-8")
.WithBodyAsJson(new
{
temp = 298.48,
feels_like = 298.74,
temp_min = 297.56,
temp_max = 300.05,
pressure = 1015,
humidity = 64
})
);
// Automate tested app
}
〰️〰️〰️〰️〰️〰️
#UnitTest #DotNet #CSharp
@ProgrammingTip
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
Programming Tips Resources