How to print information in .net core test projects?
What's the standard way of printing messages in a test project?
I have an Integration test project written in MSTest and each test case contains login step and the actual call etc. I need to print some messages to console to track information like which role it login as and which url it's calling.
Ideally I want the messages to show only in Debug mode.
Currently I am using Console.WriteLine with #if DEBUG directive. Is there any better way of doing it?
logging .net-core mstest
|
show 2 more comments
What's the standard way of printing messages in a test project?
I have an Integration test project written in MSTest and each test case contains login step and the actual call etc. I need to print some messages to console to track information like which role it login as and which url it's calling.
Ideally I want the messages to show only in Debug mode.
Currently I am using Console.WriteLine with #if DEBUG directive. Is there any better way of doing it?
logging .net-core mstest
How come you’re needing logging in a test project?
– Coops
Nov 20 '18 at 22:55
If logging is not expected for test projects, what's the preferred ways of writing messages to console or any file in a test project? I actually only want to find a way to print messages that show in debug mode only.
– Cinny
Nov 21 '18 at 16:01
The whole idea is that individual tests are that small and discreet that they are self explanatory and some form off logging is not needed. If you have too much going on then it's time to refactor and break it all down in to smaller chunks of code. What is it you are trying to output to the console and why?
– Coops
Nov 21 '18 at 16:25
I have login in [TestInitialize] and the actual request in the [TestMethod]. During the login, I printed so that I could know which environment and which role I am login as and during the actual request, I also print so that I can look at the target url of the request. These information are useful for me when a test fail so I'd like to keep them.
– Cinny
Nov 21 '18 at 18:11
1
Are you sure that you're doing unit testing and not integration testing? Unit tests are small individual pieces of code that will run and give you results, the different test runners (VS, Resharper, NUnit, etc) will give detailed information when a test fails and why they have failed. If you're doing integration testing then you don't need a special test project and then can use whatever logging framework you wish.
– Coops
Nov 22 '18 at 8:02
|
show 2 more comments
What's the standard way of printing messages in a test project?
I have an Integration test project written in MSTest and each test case contains login step and the actual call etc. I need to print some messages to console to track information like which role it login as and which url it's calling.
Ideally I want the messages to show only in Debug mode.
Currently I am using Console.WriteLine with #if DEBUG directive. Is there any better way of doing it?
logging .net-core mstest
What's the standard way of printing messages in a test project?
I have an Integration test project written in MSTest and each test case contains login step and the actual call etc. I need to print some messages to console to track information like which role it login as and which url it's calling.
Ideally I want the messages to show only in Debug mode.
Currently I am using Console.WriteLine with #if DEBUG directive. Is there any better way of doing it?
logging .net-core mstest
logging .net-core mstest
edited Nov 21 '18 at 22:39
Cinny
asked Nov 20 '18 at 22:39
CinnyCinny
95
95
How come you’re needing logging in a test project?
– Coops
Nov 20 '18 at 22:55
If logging is not expected for test projects, what's the preferred ways of writing messages to console or any file in a test project? I actually only want to find a way to print messages that show in debug mode only.
– Cinny
Nov 21 '18 at 16:01
The whole idea is that individual tests are that small and discreet that they are self explanatory and some form off logging is not needed. If you have too much going on then it's time to refactor and break it all down in to smaller chunks of code. What is it you are trying to output to the console and why?
– Coops
Nov 21 '18 at 16:25
I have login in [TestInitialize] and the actual request in the [TestMethod]. During the login, I printed so that I could know which environment and which role I am login as and during the actual request, I also print so that I can look at the target url of the request. These information are useful for me when a test fail so I'd like to keep them.
– Cinny
Nov 21 '18 at 18:11
1
Are you sure that you're doing unit testing and not integration testing? Unit tests are small individual pieces of code that will run and give you results, the different test runners (VS, Resharper, NUnit, etc) will give detailed information when a test fails and why they have failed. If you're doing integration testing then you don't need a special test project and then can use whatever logging framework you wish.
– Coops
Nov 22 '18 at 8:02
|
show 2 more comments
How come you’re needing logging in a test project?
– Coops
Nov 20 '18 at 22:55
If logging is not expected for test projects, what's the preferred ways of writing messages to console or any file in a test project? I actually only want to find a way to print messages that show in debug mode only.
– Cinny
Nov 21 '18 at 16:01
The whole idea is that individual tests are that small and discreet that they are self explanatory and some form off logging is not needed. If you have too much going on then it's time to refactor and break it all down in to smaller chunks of code. What is it you are trying to output to the console and why?
– Coops
Nov 21 '18 at 16:25
I have login in [TestInitialize] and the actual request in the [TestMethod]. During the login, I printed so that I could know which environment and which role I am login as and during the actual request, I also print so that I can look at the target url of the request. These information are useful for me when a test fail so I'd like to keep them.
– Cinny
Nov 21 '18 at 18:11
1
Are you sure that you're doing unit testing and not integration testing? Unit tests are small individual pieces of code that will run and give you results, the different test runners (VS, Resharper, NUnit, etc) will give detailed information when a test fails and why they have failed. If you're doing integration testing then you don't need a special test project and then can use whatever logging framework you wish.
– Coops
Nov 22 '18 at 8:02
How come you’re needing logging in a test project?
– Coops
Nov 20 '18 at 22:55
How come you’re needing logging in a test project?
– Coops
Nov 20 '18 at 22:55
If logging is not expected for test projects, what's the preferred ways of writing messages to console or any file in a test project? I actually only want to find a way to print messages that show in debug mode only.
– Cinny
Nov 21 '18 at 16:01
If logging is not expected for test projects, what's the preferred ways of writing messages to console or any file in a test project? I actually only want to find a way to print messages that show in debug mode only.
– Cinny
Nov 21 '18 at 16:01
The whole idea is that individual tests are that small and discreet that they are self explanatory and some form off logging is not needed. If you have too much going on then it's time to refactor and break it all down in to smaller chunks of code. What is it you are trying to output to the console and why?
– Coops
Nov 21 '18 at 16:25
The whole idea is that individual tests are that small and discreet that they are self explanatory and some form off logging is not needed. If you have too much going on then it's time to refactor and break it all down in to smaller chunks of code. What is it you are trying to output to the console and why?
– Coops
Nov 21 '18 at 16:25
I have login in [TestInitialize] and the actual request in the [TestMethod]. During the login, I printed so that I could know which environment and which role I am login as and during the actual request, I also print so that I can look at the target url of the request. These information are useful for me when a test fail so I'd like to keep them.
– Cinny
Nov 21 '18 at 18:11
I have login in [TestInitialize] and the actual request in the [TestMethod]. During the login, I printed so that I could know which environment and which role I am login as and during the actual request, I also print so that I can look at the target url of the request. These information are useful for me when a test fail so I'd like to keep them.
– Cinny
Nov 21 '18 at 18:11
1
1
Are you sure that you're doing unit testing and not integration testing? Unit tests are small individual pieces of code that will run and give you results, the different test runners (VS, Resharper, NUnit, etc) will give detailed information when a test fails and why they have failed. If you're doing integration testing then you don't need a special test project and then can use whatever logging framework you wish.
– Coops
Nov 22 '18 at 8:02
Are you sure that you're doing unit testing and not integration testing? Unit tests are small individual pieces of code that will run and give you results, the different test runners (VS, Resharper, NUnit, etc) will give detailed information when a test fails and why they have failed. If you're doing integration testing then you don't need a special test project and then can use whatever logging framework you wish.
– Coops
Nov 22 '18 at 8:02
|
show 2 more comments
1 Answer
1
active
oldest
votes
You shouldn't need to log anything in your unit tests. The premise being that the code in the tests speak for themselves and anyone reading them can understand what is going. If you feel the need to add logging to your tests then that is a sign that your code is too complicated and needs to be refactored.
It's a good idea to understand the principles behind SOLID principles and refactor your code to follow these "rules". I would recommend the first principle you try to follow is the Single Responsibility Principle. This should help you refactor your code to make it more testable and remove the need to add logging to your tests. (NOTE: There are other examples on the internet - the links above are just examples I found with a quick search).
EDIT
You could create your own logger class to wrap your Console.WriteLine(...) calls e.g.
public static class TestLogger
{
public static void Log(string output)
{
#if DEBUG
Console.WriteLine(output);
#endif
}
}
and in your test code use your class
public class MyTests
{
public void FirstTest()
{
TestLogger.Log("Just started");
}
}
The fact that the logger only writes to the console in debug mode is hidden from the test code and should hopefully make it a little clearer. In the future, if you want to use Log4Net or another logging library you can update your TestLogger class and, hopefully, none of your test cases need changing :-)
Emm, it's an integration test project and the test case involve multiple steps like login, sending the request and logout etc. So I want to print to the console to track the progress of each test. Is doing logging in a test project not recommended or impossible? As long as it's possible I'd like to know.
– Cinny
Nov 21 '18 at 15:44
1
You didn’t mention any of that in your question. Can i suggest you take look at How to Ask and update your question with all the revelant information i.e why you want to do logging in your tests. It will help people provide better answers. Thanks
– Simply Ged
Nov 21 '18 at 20:03
Question reworded. Hope this is more clear
– Cinny
Nov 21 '18 at 22:44
I've updated my answer with a possible solution to make your code cleaner
– Simply Ged
Nov 22 '18 at 4:59
Emm, thank you, this wrapper is worthy adding.
– Cinny
Nov 22 '18 at 15:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402660%2fhow-to-print-information-in-net-core-test-projects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You shouldn't need to log anything in your unit tests. The premise being that the code in the tests speak for themselves and anyone reading them can understand what is going. If you feel the need to add logging to your tests then that is a sign that your code is too complicated and needs to be refactored.
It's a good idea to understand the principles behind SOLID principles and refactor your code to follow these "rules". I would recommend the first principle you try to follow is the Single Responsibility Principle. This should help you refactor your code to make it more testable and remove the need to add logging to your tests. (NOTE: There are other examples on the internet - the links above are just examples I found with a quick search).
EDIT
You could create your own logger class to wrap your Console.WriteLine(...) calls e.g.
public static class TestLogger
{
public static void Log(string output)
{
#if DEBUG
Console.WriteLine(output);
#endif
}
}
and in your test code use your class
public class MyTests
{
public void FirstTest()
{
TestLogger.Log("Just started");
}
}
The fact that the logger only writes to the console in debug mode is hidden from the test code and should hopefully make it a little clearer. In the future, if you want to use Log4Net or another logging library you can update your TestLogger class and, hopefully, none of your test cases need changing :-)
Emm, it's an integration test project and the test case involve multiple steps like login, sending the request and logout etc. So I want to print to the console to track the progress of each test. Is doing logging in a test project not recommended or impossible? As long as it's possible I'd like to know.
– Cinny
Nov 21 '18 at 15:44
1
You didn’t mention any of that in your question. Can i suggest you take look at How to Ask and update your question with all the revelant information i.e why you want to do logging in your tests. It will help people provide better answers. Thanks
– Simply Ged
Nov 21 '18 at 20:03
Question reworded. Hope this is more clear
– Cinny
Nov 21 '18 at 22:44
I've updated my answer with a possible solution to make your code cleaner
– Simply Ged
Nov 22 '18 at 4:59
Emm, thank you, this wrapper is worthy adding.
– Cinny
Nov 22 '18 at 15:00
add a comment |
You shouldn't need to log anything in your unit tests. The premise being that the code in the tests speak for themselves and anyone reading them can understand what is going. If you feel the need to add logging to your tests then that is a sign that your code is too complicated and needs to be refactored.
It's a good idea to understand the principles behind SOLID principles and refactor your code to follow these "rules". I would recommend the first principle you try to follow is the Single Responsibility Principle. This should help you refactor your code to make it more testable and remove the need to add logging to your tests. (NOTE: There are other examples on the internet - the links above are just examples I found with a quick search).
EDIT
You could create your own logger class to wrap your Console.WriteLine(...) calls e.g.
public static class TestLogger
{
public static void Log(string output)
{
#if DEBUG
Console.WriteLine(output);
#endif
}
}
and in your test code use your class
public class MyTests
{
public void FirstTest()
{
TestLogger.Log("Just started");
}
}
The fact that the logger only writes to the console in debug mode is hidden from the test code and should hopefully make it a little clearer. In the future, if you want to use Log4Net or another logging library you can update your TestLogger class and, hopefully, none of your test cases need changing :-)
Emm, it's an integration test project and the test case involve multiple steps like login, sending the request and logout etc. So I want to print to the console to track the progress of each test. Is doing logging in a test project not recommended or impossible? As long as it's possible I'd like to know.
– Cinny
Nov 21 '18 at 15:44
1
You didn’t mention any of that in your question. Can i suggest you take look at How to Ask and update your question with all the revelant information i.e why you want to do logging in your tests. It will help people provide better answers. Thanks
– Simply Ged
Nov 21 '18 at 20:03
Question reworded. Hope this is more clear
– Cinny
Nov 21 '18 at 22:44
I've updated my answer with a possible solution to make your code cleaner
– Simply Ged
Nov 22 '18 at 4:59
Emm, thank you, this wrapper is worthy adding.
– Cinny
Nov 22 '18 at 15:00
add a comment |
You shouldn't need to log anything in your unit tests. The premise being that the code in the tests speak for themselves and anyone reading them can understand what is going. If you feel the need to add logging to your tests then that is a sign that your code is too complicated and needs to be refactored.
It's a good idea to understand the principles behind SOLID principles and refactor your code to follow these "rules". I would recommend the first principle you try to follow is the Single Responsibility Principle. This should help you refactor your code to make it more testable and remove the need to add logging to your tests. (NOTE: There are other examples on the internet - the links above are just examples I found with a quick search).
EDIT
You could create your own logger class to wrap your Console.WriteLine(...) calls e.g.
public static class TestLogger
{
public static void Log(string output)
{
#if DEBUG
Console.WriteLine(output);
#endif
}
}
and in your test code use your class
public class MyTests
{
public void FirstTest()
{
TestLogger.Log("Just started");
}
}
The fact that the logger only writes to the console in debug mode is hidden from the test code and should hopefully make it a little clearer. In the future, if you want to use Log4Net or another logging library you can update your TestLogger class and, hopefully, none of your test cases need changing :-)
You shouldn't need to log anything in your unit tests. The premise being that the code in the tests speak for themselves and anyone reading them can understand what is going. If you feel the need to add logging to your tests then that is a sign that your code is too complicated and needs to be refactored.
It's a good idea to understand the principles behind SOLID principles and refactor your code to follow these "rules". I would recommend the first principle you try to follow is the Single Responsibility Principle. This should help you refactor your code to make it more testable and remove the need to add logging to your tests. (NOTE: There are other examples on the internet - the links above are just examples I found with a quick search).
EDIT
You could create your own logger class to wrap your Console.WriteLine(...) calls e.g.
public static class TestLogger
{
public static void Log(string output)
{
#if DEBUG
Console.WriteLine(output);
#endif
}
}
and in your test code use your class
public class MyTests
{
public void FirstTest()
{
TestLogger.Log("Just started");
}
}
The fact that the logger only writes to the console in debug mode is hidden from the test code and should hopefully make it a little clearer. In the future, if you want to use Log4Net or another logging library you can update your TestLogger class and, hopefully, none of your test cases need changing :-)
edited Nov 22 '18 at 4:59
answered Nov 20 '18 at 22:56
Simply GedSimply Ged
2,39321321
2,39321321
Emm, it's an integration test project and the test case involve multiple steps like login, sending the request and logout etc. So I want to print to the console to track the progress of each test. Is doing logging in a test project not recommended or impossible? As long as it's possible I'd like to know.
– Cinny
Nov 21 '18 at 15:44
1
You didn’t mention any of that in your question. Can i suggest you take look at How to Ask and update your question with all the revelant information i.e why you want to do logging in your tests. It will help people provide better answers. Thanks
– Simply Ged
Nov 21 '18 at 20:03
Question reworded. Hope this is more clear
– Cinny
Nov 21 '18 at 22:44
I've updated my answer with a possible solution to make your code cleaner
– Simply Ged
Nov 22 '18 at 4:59
Emm, thank you, this wrapper is worthy adding.
– Cinny
Nov 22 '18 at 15:00
add a comment |
Emm, it's an integration test project and the test case involve multiple steps like login, sending the request and logout etc. So I want to print to the console to track the progress of each test. Is doing logging in a test project not recommended or impossible? As long as it's possible I'd like to know.
– Cinny
Nov 21 '18 at 15:44
1
You didn’t mention any of that in your question. Can i suggest you take look at How to Ask and update your question with all the revelant information i.e why you want to do logging in your tests. It will help people provide better answers. Thanks
– Simply Ged
Nov 21 '18 at 20:03
Question reworded. Hope this is more clear
– Cinny
Nov 21 '18 at 22:44
I've updated my answer with a possible solution to make your code cleaner
– Simply Ged
Nov 22 '18 at 4:59
Emm, thank you, this wrapper is worthy adding.
– Cinny
Nov 22 '18 at 15:00
Emm, it's an integration test project and the test case involve multiple steps like login, sending the request and logout etc. So I want to print to the console to track the progress of each test. Is doing logging in a test project not recommended or impossible? As long as it's possible I'd like to know.
– Cinny
Nov 21 '18 at 15:44
Emm, it's an integration test project and the test case involve multiple steps like login, sending the request and logout etc. So I want to print to the console to track the progress of each test. Is doing logging in a test project not recommended or impossible? As long as it's possible I'd like to know.
– Cinny
Nov 21 '18 at 15:44
1
1
You didn’t mention any of that in your question. Can i suggest you take look at How to Ask and update your question with all the revelant information i.e why you want to do logging in your tests. It will help people provide better answers. Thanks
– Simply Ged
Nov 21 '18 at 20:03
You didn’t mention any of that in your question. Can i suggest you take look at How to Ask and update your question with all the revelant information i.e why you want to do logging in your tests. It will help people provide better answers. Thanks
– Simply Ged
Nov 21 '18 at 20:03
Question reworded. Hope this is more clear
– Cinny
Nov 21 '18 at 22:44
Question reworded. Hope this is more clear
– Cinny
Nov 21 '18 at 22:44
I've updated my answer with a possible solution to make your code cleaner
– Simply Ged
Nov 22 '18 at 4:59
I've updated my answer with a possible solution to make your code cleaner
– Simply Ged
Nov 22 '18 at 4:59
Emm, thank you, this wrapper is worthy adding.
– Cinny
Nov 22 '18 at 15:00
Emm, thank you, this wrapper is worthy adding.
– Cinny
Nov 22 '18 at 15:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402660%2fhow-to-print-information-in-net-core-test-projects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How come you’re needing logging in a test project?
– Coops
Nov 20 '18 at 22:55
If logging is not expected for test projects, what's the preferred ways of writing messages to console or any file in a test project? I actually only want to find a way to print messages that show in debug mode only.
– Cinny
Nov 21 '18 at 16:01
The whole idea is that individual tests are that small and discreet that they are self explanatory and some form off logging is not needed. If you have too much going on then it's time to refactor and break it all down in to smaller chunks of code. What is it you are trying to output to the console and why?
– Coops
Nov 21 '18 at 16:25
I have login in [TestInitialize] and the actual request in the [TestMethod]. During the login, I printed so that I could know which environment and which role I am login as and during the actual request, I also print so that I can look at the target url of the request. These information are useful for me when a test fail so I'd like to keep them.
– Cinny
Nov 21 '18 at 18:11
1
Are you sure that you're doing unit testing and not integration testing? Unit tests are small individual pieces of code that will run and give you results, the different test runners (VS, Resharper, NUnit, etc) will give detailed information when a test fails and why they have failed. If you're doing integration testing then you don't need a special test project and then can use whatever logging framework you wish.
– Coops
Nov 22 '18 at 8:02