Check if the running code is unit test case or not

跟風遠走 提交于 2019-12-23 09:55:52

问题


I want to check if the running code is unit tese case or not execute different code for the result such as:

    if ( unit test case is running )
{
do something
}
else
{
do other thing
}

any idea on this?


回答1:


This is a bad approach you should try to simulate logic parts which you are trying to avoid by this statemetn through Mock ojects or other mechanism.

Now to your question you can use a oolean variable like isUnittest which you set on test setup and Teardown, ut as saied i dont recommend you doing this.




回答2:


Another way is to let the class have customizable behavior controlled via a static method, and have test case call that method in its static load method.

I had a similar issue using storyboard and an external restful service for authentication via oauth. The app delegate would check if there is a valid oauth token in appdelegate:didFinishLaunchingWithOptions, and if not, then programatically trigger segue to do the oauth login. But this was not desirable in test cases. To solve this, I created a static method within the app delegate to disable the login screen. This is the code within my app delegate:

static Boolean showLoginScreen = TRUE ;

+ (void) disableLoginScreen
{
    showLoginScreen = FALSE ;
    NSLog(@"disabled login screen") ;
}

The test case had its load method to do the following:

// disable login screen for the test case
+ (void) load {
    NSLog( @"now disabling login screen" ) ;
   [XYZAppDelegate disableLoginScreen];
}

This worked because the test case class was loaded before application was initialized. Of course you must check the value of this flag within app delegate to trigger/not trigger the login segue. Other alternatives I tried but rejected were as follows:

  • Create a preprocessor define on the test target. But the compiler only compiles the test case files with this flag, not the application source. See http://www.innovaptor.com/blog/2013/09/02/xcode-preprocessor-macros-for-test-code.html

  • Use the static initialize method of the test case to call the disable method. On test case run, the application is started before the test case class is loaded. See http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/ for some details.




回答3:


This seems to work for me (iOS 8, Xcode 6):

- (BOOL) isRunningTest {
    return NSClassFromString(@"XCTestCase") != nil;
}

I think this is cleaner and easier than other answers.




回答4:


Don't message UIAlertView directly. Instead, use dependency injection, for example, a property like

@property (strong, nonatomic) Class alertViewClass;

Then your code to create an alert can do

UIAlertView *alert = [[_alertViewClass alloc] initWithTitle:…etc…];

In your test code, inject a different class. I use https://github.com/jonreid/JMRTestTools to specify JMRMockAlertView. I can then test the alert calls with JMRMockAlertViewVerifier. (In fact, this enables test-driven development of alerts.)

Edit: These days, I use https://github.com/jonreid/ViewControllerPresentationSpy



来源:https://stackoverflow.com/questions/14333119/check-if-the-running-code-is-unit-test-case-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!