How to test NSLog in testing framework (default iOS)How should I unit test threaded code?How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?How can I make a UITextField move up when the keyboard is present - on starting to edit?iOS Tests/Specs TDD/BDD and Integration & Acceptance TestingTest Driven Development (TDD) for User Interface (UI) with functional testsConfusion about unit testing frameworks?
Must a CPU have a GPU if the motherboard provides a display port (when there isn't any separate video card)?
Nth term of Van Eck Sequence
Can a 40amp breaker be used safely and without issue with a 40amp device on 6AWG wire?
Background for black and white chart
Placement of positioning lights on A320 winglets
Approach sick days in feedback meeting
Has JSON.serialize suppressApexObjectNulls ever worked?
Would a bit of grease on overhead door cables or bearings cause the springs to break?
Purpose of cylindrical attachments on Power Transmission towers
What publication claimed that Michael Jackson died in a nuclear holocaust?
Is pointing finger in meeting consider bad?
Dedicated bike GPS computer over smartphone
Someone who is granted access to information but not expected to read it
Why are backslashes included in this shell script?
What's the reason for the decade jump in the recent X-Men trilogy?
Is it possible to have battery technology that can't be duplicated?
usage of mir gefallen
Opposite of "Concerto Grosso"?
How to search for Android apps without ads?
Does an African-American baby born in Youngstown, Ohio have a higher infant mortality rate than a baby born in Iran?
Why does this Apple //e drops into system monitor when booting?
Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?
Manager wants to hire me; HR does not. How to proceed?
The best in flight meal option for those suffering from reflux
How to test NSLog in testing framework (default iOS)
How should I unit test threaded code?How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?How can I make a UITextField move up when the keyboard is present - on starting to edit?iOS Tests/Specs TDD/BDD and Integration & Acceptance TestingTest Driven Development (TDD) for User Interface (UI) with functional testsConfusion about unit testing frameworks?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Does anyone know how to write a unit test for NSLog
functionality?
I am writing unit tests for all my framework components and it's very import to me to test if logging occurs in various scenarios.
Ex: A configuration that allows a user of your framework to enable or disable logging. It is important that logging occurs if they enable it but this is difficult to verify in unit tests.
Can someone please help me?
ios unit-testing
|
show 2 more comments
Does anyone know how to write a unit test for NSLog
functionality?
I am writing unit tests for all my framework components and it's very import to me to test if logging occurs in various scenarios.
Ex: A configuration that allows a user of your framework to enable or disable logging. It is important that logging occurs if they enable it but this is difficult to verify in unit tests.
Can someone please help me?
ios unit-testing
My question is, when you do NSLog how do I write a unit test case that will tell me does nslog printed on console or not ? Does that explain my question @H2CO3.
– AAV
Nov 26 '12 at 15:49
It doesn't, sinceNSLog()
always prints if you call it...
– user529758
Nov 26 '12 at 15:57
Maybe you can replace the NSLog implementation with a macro # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTION, LINE, ##__VA_ARGS__);
– Scott Bossak
Nov 26 '12 at 16:16
3
@ScottBossak - You should have used acute accents around that line -- most of your underscores turned into bold characters.
– Hot Licks
Nov 26 '12 at 16:43
1
@Joe You make a compelling case. I've re-opened. Please submit an edit if you can clarify the question further.
– Cody Gray♦
Mar 24 at 0:22
|
show 2 more comments
Does anyone know how to write a unit test for NSLog
functionality?
I am writing unit tests for all my framework components and it's very import to me to test if logging occurs in various scenarios.
Ex: A configuration that allows a user of your framework to enable or disable logging. It is important that logging occurs if they enable it but this is difficult to verify in unit tests.
Can someone please help me?
ios unit-testing
Does anyone know how to write a unit test for NSLog
functionality?
I am writing unit tests for all my framework components and it's very import to me to test if logging occurs in various scenarios.
Ex: A configuration that allows a user of your framework to enable or disable logging. It is important that logging occurs if they enable it but this is difficult to verify in unit tests.
Can someone please help me?
ios unit-testing
ios unit-testing
edited Mar 25 at 1:42
Joe Susnick
2,90822735
2,90822735
asked Nov 26 '12 at 15:36
AAVAAV
2,44152552
2,44152552
My question is, when you do NSLog how do I write a unit test case that will tell me does nslog printed on console or not ? Does that explain my question @H2CO3.
– AAV
Nov 26 '12 at 15:49
It doesn't, sinceNSLog()
always prints if you call it...
– user529758
Nov 26 '12 at 15:57
Maybe you can replace the NSLog implementation with a macro # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTION, LINE, ##__VA_ARGS__);
– Scott Bossak
Nov 26 '12 at 16:16
3
@ScottBossak - You should have used acute accents around that line -- most of your underscores turned into bold characters.
– Hot Licks
Nov 26 '12 at 16:43
1
@Joe You make a compelling case. I've re-opened. Please submit an edit if you can clarify the question further.
– Cody Gray♦
Mar 24 at 0:22
|
show 2 more comments
My question is, when you do NSLog how do I write a unit test case that will tell me does nslog printed on console or not ? Does that explain my question @H2CO3.
– AAV
Nov 26 '12 at 15:49
It doesn't, sinceNSLog()
always prints if you call it...
– user529758
Nov 26 '12 at 15:57
Maybe you can replace the NSLog implementation with a macro # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTION, LINE, ##__VA_ARGS__);
– Scott Bossak
Nov 26 '12 at 16:16
3
@ScottBossak - You should have used acute accents around that line -- most of your underscores turned into bold characters.
– Hot Licks
Nov 26 '12 at 16:43
1
@Joe You make a compelling case. I've re-opened. Please submit an edit if you can clarify the question further.
– Cody Gray♦
Mar 24 at 0:22
My question is, when you do NSLog how do I write a unit test case that will tell me does nslog printed on console or not ? Does that explain my question @H2CO3.
– AAV
Nov 26 '12 at 15:49
My question is, when you do NSLog how do I write a unit test case that will tell me does nslog printed on console or not ? Does that explain my question @H2CO3.
– AAV
Nov 26 '12 at 15:49
It doesn't, since
NSLog()
always prints if you call it...– user529758
Nov 26 '12 at 15:57
It doesn't, since
NSLog()
always prints if you call it...– user529758
Nov 26 '12 at 15:57
Maybe you can replace the NSLog implementation with a macro # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTION, LINE, ##__VA_ARGS__);
– Scott Bossak
Nov 26 '12 at 16:16
Maybe you can replace the NSLog implementation with a macro # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTION, LINE, ##__VA_ARGS__);
– Scott Bossak
Nov 26 '12 at 16:16
3
3
@ScottBossak - You should have used acute accents around that line -- most of your underscores turned into bold characters.
– Hot Licks
Nov 26 '12 at 16:43
@ScottBossak - You should have used acute accents around that line -- most of your underscores turned into bold characters.
– Hot Licks
Nov 26 '12 at 16:43
1
1
@Joe You make a compelling case. I've re-opened. Please submit an edit if you can clarify the question further.
– Cody Gray♦
Mar 24 at 0:22
@Joe You make a compelling case. I've re-opened. Please submit an edit if you can clarify the question further.
– Cody Gray♦
Mar 24 at 0:22
|
show 2 more comments
2 Answers
2
active
oldest
votes
Simple way to test is, do NSLog and then check the console log. And code to check that
NSMutableArray *consoleLog = [NSMutableArray array];
aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);
aslmsg query = asl_new(ASL_TYPE_QUERY);
asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
aslresponse response = asl_search(client, query);
asl_free(query);
aslmsg message;
while((message = aslresponse_next(response)))
const char *msg = asl_get(message, ASL_KEY_MSG);
[consoleLog addObject:[NSString stringWithFormat:@"%s" , msg]];
NSLog(@"Console: %@", consoleLog);
add a comment |
I would recommend writing a wrapper for NSLog and testing that wrapper thoroughly. Example: you have a class URLStringValidator
that exists to validate url strings.
You want to log a message if an empty String
is passed in. You would make a protocol to represent your logger.
protocol Logging
func log(_ message: String, args: CVarArg...)
struct Logger: Logging
func log(_ message: String, args: CVarArg...)
NSLog(message, args)
class URLValidator
let logger: Logging
init(logger: Logging = Logger())
self.logger = logger
func isEmpty(urlString: String) -> Bool
guard !urlString.isEmpty else
logger.log("Don't pass an empty string")
return false
return true
Now you can use a fake instead of Logger
. I would also argue that you need an additional test to assert that logger has a concrete implementation of the correct type. ex:
class FakeLogger: Logging
var capturedMessage: String?
func log(_ message: String, args: CVarArg...)
capturedMessage = message
class URLValidatorTests
func testLoggerDefaults()
XCTAssertTrue(URLValidator().logger is Logger,
"A url validator should use the correct concrete class for its logger")
func testValidatingEmptyString()
let fakeLogger = FakeLogger()
let validator = URLValidator(logger: fakeLogger)
validator.isEmpty(urlString: "")
XCTAssertNil(fakeLogger.capturedMessage)
validator.isEmpty(urlString: "Not Empty")
XCTAssertEqual(fakeLogger.capturedMessage, "Don't pass an empty string")
Now you can test your logic (the part you care about and should be testing) and leave NSLog
as a configuration detail. You could just as easy use os_log
or write to a custom file.
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%2f13568075%2fhow-to-test-nslog-in-testing-framework-default-ios%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simple way to test is, do NSLog and then check the console log. And code to check that
NSMutableArray *consoleLog = [NSMutableArray array];
aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);
aslmsg query = asl_new(ASL_TYPE_QUERY);
asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
aslresponse response = asl_search(client, query);
asl_free(query);
aslmsg message;
while((message = aslresponse_next(response)))
const char *msg = asl_get(message, ASL_KEY_MSG);
[consoleLog addObject:[NSString stringWithFormat:@"%s" , msg]];
NSLog(@"Console: %@", consoleLog);
add a comment |
Simple way to test is, do NSLog and then check the console log. And code to check that
NSMutableArray *consoleLog = [NSMutableArray array];
aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);
aslmsg query = asl_new(ASL_TYPE_QUERY);
asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
aslresponse response = asl_search(client, query);
asl_free(query);
aslmsg message;
while((message = aslresponse_next(response)))
const char *msg = asl_get(message, ASL_KEY_MSG);
[consoleLog addObject:[NSString stringWithFormat:@"%s" , msg]];
NSLog(@"Console: %@", consoleLog);
add a comment |
Simple way to test is, do NSLog and then check the console log. And code to check that
NSMutableArray *consoleLog = [NSMutableArray array];
aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);
aslmsg query = asl_new(ASL_TYPE_QUERY);
asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
aslresponse response = asl_search(client, query);
asl_free(query);
aslmsg message;
while((message = aslresponse_next(response)))
const char *msg = asl_get(message, ASL_KEY_MSG);
[consoleLog addObject:[NSString stringWithFormat:@"%s" , msg]];
NSLog(@"Console: %@", consoleLog);
Simple way to test is, do NSLog and then check the console log. And code to check that
NSMutableArray *consoleLog = [NSMutableArray array];
aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);
aslmsg query = asl_new(ASL_TYPE_QUERY);
asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
aslresponse response = asl_search(client, query);
asl_free(query);
aslmsg message;
while((message = aslresponse_next(response)))
const char *msg = asl_get(message, ASL_KEY_MSG);
[consoleLog addObject:[NSString stringWithFormat:@"%s" , msg]];
NSLog(@"Console: %@", consoleLog);
edited May 28 '13 at 21:19
answered Nov 26 '12 at 16:30
AAVAAV
2,44152552
2,44152552
add a comment |
add a comment |
I would recommend writing a wrapper for NSLog and testing that wrapper thoroughly. Example: you have a class URLStringValidator
that exists to validate url strings.
You want to log a message if an empty String
is passed in. You would make a protocol to represent your logger.
protocol Logging
func log(_ message: String, args: CVarArg...)
struct Logger: Logging
func log(_ message: String, args: CVarArg...)
NSLog(message, args)
class URLValidator
let logger: Logging
init(logger: Logging = Logger())
self.logger = logger
func isEmpty(urlString: String) -> Bool
guard !urlString.isEmpty else
logger.log("Don't pass an empty string")
return false
return true
Now you can use a fake instead of Logger
. I would also argue that you need an additional test to assert that logger has a concrete implementation of the correct type. ex:
class FakeLogger: Logging
var capturedMessage: String?
func log(_ message: String, args: CVarArg...)
capturedMessage = message
class URLValidatorTests
func testLoggerDefaults()
XCTAssertTrue(URLValidator().logger is Logger,
"A url validator should use the correct concrete class for its logger")
func testValidatingEmptyString()
let fakeLogger = FakeLogger()
let validator = URLValidator(logger: fakeLogger)
validator.isEmpty(urlString: "")
XCTAssertNil(fakeLogger.capturedMessage)
validator.isEmpty(urlString: "Not Empty")
XCTAssertEqual(fakeLogger.capturedMessage, "Don't pass an empty string")
Now you can test your logic (the part you care about and should be testing) and leave NSLog
as a configuration detail. You could just as easy use os_log
or write to a custom file.
add a comment |
I would recommend writing a wrapper for NSLog and testing that wrapper thoroughly. Example: you have a class URLStringValidator
that exists to validate url strings.
You want to log a message if an empty String
is passed in. You would make a protocol to represent your logger.
protocol Logging
func log(_ message: String, args: CVarArg...)
struct Logger: Logging
func log(_ message: String, args: CVarArg...)
NSLog(message, args)
class URLValidator
let logger: Logging
init(logger: Logging = Logger())
self.logger = logger
func isEmpty(urlString: String) -> Bool
guard !urlString.isEmpty else
logger.log("Don't pass an empty string")
return false
return true
Now you can use a fake instead of Logger
. I would also argue that you need an additional test to assert that logger has a concrete implementation of the correct type. ex:
class FakeLogger: Logging
var capturedMessage: String?
func log(_ message: String, args: CVarArg...)
capturedMessage = message
class URLValidatorTests
func testLoggerDefaults()
XCTAssertTrue(URLValidator().logger is Logger,
"A url validator should use the correct concrete class for its logger")
func testValidatingEmptyString()
let fakeLogger = FakeLogger()
let validator = URLValidator(logger: fakeLogger)
validator.isEmpty(urlString: "")
XCTAssertNil(fakeLogger.capturedMessage)
validator.isEmpty(urlString: "Not Empty")
XCTAssertEqual(fakeLogger.capturedMessage, "Don't pass an empty string")
Now you can test your logic (the part you care about and should be testing) and leave NSLog
as a configuration detail. You could just as easy use os_log
or write to a custom file.
add a comment |
I would recommend writing a wrapper for NSLog and testing that wrapper thoroughly. Example: you have a class URLStringValidator
that exists to validate url strings.
You want to log a message if an empty String
is passed in. You would make a protocol to represent your logger.
protocol Logging
func log(_ message: String, args: CVarArg...)
struct Logger: Logging
func log(_ message: String, args: CVarArg...)
NSLog(message, args)
class URLValidator
let logger: Logging
init(logger: Logging = Logger())
self.logger = logger
func isEmpty(urlString: String) -> Bool
guard !urlString.isEmpty else
logger.log("Don't pass an empty string")
return false
return true
Now you can use a fake instead of Logger
. I would also argue that you need an additional test to assert that logger has a concrete implementation of the correct type. ex:
class FakeLogger: Logging
var capturedMessage: String?
func log(_ message: String, args: CVarArg...)
capturedMessage = message
class URLValidatorTests
func testLoggerDefaults()
XCTAssertTrue(URLValidator().logger is Logger,
"A url validator should use the correct concrete class for its logger")
func testValidatingEmptyString()
let fakeLogger = FakeLogger()
let validator = URLValidator(logger: fakeLogger)
validator.isEmpty(urlString: "")
XCTAssertNil(fakeLogger.capturedMessage)
validator.isEmpty(urlString: "Not Empty")
XCTAssertEqual(fakeLogger.capturedMessage, "Don't pass an empty string")
Now you can test your logic (the part you care about and should be testing) and leave NSLog
as a configuration detail. You could just as easy use os_log
or write to a custom file.
I would recommend writing a wrapper for NSLog and testing that wrapper thoroughly. Example: you have a class URLStringValidator
that exists to validate url strings.
You want to log a message if an empty String
is passed in. You would make a protocol to represent your logger.
protocol Logging
func log(_ message: String, args: CVarArg...)
struct Logger: Logging
func log(_ message: String, args: CVarArg...)
NSLog(message, args)
class URLValidator
let logger: Logging
init(logger: Logging = Logger())
self.logger = logger
func isEmpty(urlString: String) -> Bool
guard !urlString.isEmpty else
logger.log("Don't pass an empty string")
return false
return true
Now you can use a fake instead of Logger
. I would also argue that you need an additional test to assert that logger has a concrete implementation of the correct type. ex:
class FakeLogger: Logging
var capturedMessage: String?
func log(_ message: String, args: CVarArg...)
capturedMessage = message
class URLValidatorTests
func testLoggerDefaults()
XCTAssertTrue(URLValidator().logger is Logger,
"A url validator should use the correct concrete class for its logger")
func testValidatingEmptyString()
let fakeLogger = FakeLogger()
let validator = URLValidator(logger: fakeLogger)
validator.isEmpty(urlString: "")
XCTAssertNil(fakeLogger.capturedMessage)
validator.isEmpty(urlString: "Not Empty")
XCTAssertEqual(fakeLogger.capturedMessage, "Don't pass an empty string")
Now you can test your logic (the part you care about and should be testing) and leave NSLog
as a configuration detail. You could just as easy use os_log
or write to a custom file.
answered Mar 25 at 1:34
Joe SusnickJoe Susnick
2,90822735
2,90822735
add a comment |
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%2f13568075%2fhow-to-test-nslog-in-testing-framework-default-ios%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
My question is, when you do NSLog how do I write a unit test case that will tell me does nslog printed on console or not ? Does that explain my question @H2CO3.
– AAV
Nov 26 '12 at 15:49
It doesn't, since
NSLog()
always prints if you call it...– user529758
Nov 26 '12 at 15:57
Maybe you can replace the NSLog implementation with a macro # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTION, LINE, ##__VA_ARGS__);
– Scott Bossak
Nov 26 '12 at 16:16
3
@ScottBossak - You should have used acute accents around that line -- most of your underscores turned into bold characters.
– Hot Licks
Nov 26 '12 at 16:43
1
@Joe You make a compelling case. I've re-opened. Please submit an edit if you can clarify the question further.
– Cody Gray♦
Mar 24 at 0:22