TestFairy allows developers to log items with a session, without logging to the console output. In some cases, there are workarounds that allow you to wrap the TestFairy remote logging method in a way that will both log to the console and to the session.

Syntax

TestFairy.log("<tag>", "<message>");

Code Example

// Be sure to import TestFairy
import com.testfairy.TestFairy;

TestFairy.log("Tag", "Hello, TestFairy!");
      

Syntax

TFLog(@"<message with format>", <arguments>);
[TestFairy log:@"<message>"];

Code Example

// Be sure to import TestFairy
#import "TestFairy.h"

TFLog(@"Hello, %@", @"TestFairy!");
[TestFairy log:@"Hello, TestFairy!"];
      

We recommend sending all calls to NSLog to TestFairy so you can continue to use NSLog and see all your log statements in your session.

To enable sending logs to TestFairy, you will have to redefine NSLog using a macro with the following lines tThis macro allows you to continue using NSLog in your code, while also adding the logs to the matching session in TestFairy).

Changing Your Prefix Header

#import "TestFairy.h"
#define NSLog(s, ...) do { NSLog(s, ##__VA_ARGS__); TFLog(s, ##__VA_ARGS__); } while (0)
  1. Add the above line to a global header in your project, accessible to every class file.
  2. Update or create a Prefix Header (.pch) for your project. If you do not have a PCH file in your project, you can follow the steps in the next section.

Creating a New Prefix Header

If your project doesn’t already include a Prefix Header (.pch):

  1. Create a new file under iOS > Other > PCH File.
  2. Name your file “PCH file”.
  3. Add these lines of code to the file:
    #import "TestFairy.h"
    #define NSLog(s, ...) do { NSLog(s, ##__VA_ARGS__); TFLog(s, ##__VA_ARGS__); } while (0)
  4. From the Project Navigator, select your project and the corresponding target.

  5. Project > Build Settings > Search: "Prefix Header".

  6. Under Apple LLVM 7.0" you will get the Prefix Header key.

  7. Type the path of the file, for example: "$(SRCROOT)/$(PROJECT_NAME)/ProjectName-Prefix.pch". Please note that your file may be at a different location.

  8. Make sure the option "Precompile Prefix Header" is set to YES.

  9. Clean your project, and rebuild.

Syntax

TestFairy.log("<message>")

Code Example

TestFairy.log("Hello, TestFairy!")
      

We recommend wrapping all print statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to create a new file named TestFairyLog.swift in your source path, and add the following to the contents of the file:

//
//  TestFairyLog.swift
//
//  Copyright © TestFairy. All rights reserved.
//

import Foundation

public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
    let output = items.map { "\($0)" }.joined(separator: separator)
    TestFairy.log(output)
    Swift.print(output, terminator: terminator)
}

This will print any output to both the Xcode console, and to the active session on TestFairy.

Syntax


Code Example

TestFairy.log("Hello, TestFairy!");
      

We recommend wrapping all log statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:

var testfairyConsoleLog = console.log;
console.log = function(message) {
    testfairyConsoleLog(message);
    TestFairy.log(message);
}
            

Syntax

TestFairy.log("<message>");

Code Example

// Be sure to import TestFairy
const TestFairy = require('react-native-testfairy');

TestFairy.log("Hello, TestFairy!");
      

We recommend wrapping all log statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:

var testfairyConsoleLog = console.log;
console.log = function(message) {
    testfairyConsoleLog(message);
    TestFairy.log(message);
}
            

Syntax

TestFairySDK.log("<message>");

Code Example

// Be sure to import TestFairy
import { TestFairySDK } from 'nativescript-testfairy';

TestFairySDK.log("Hello, TestFairy!");
      

We recommend wrapping all log statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:

var testfairyConsoleLog = console.log;
console.log = function(message) {
    testfairyConsoleLog(message);
    TestFairySDK.log(message);
}
            

We recommend wrapping all TFLog statements with a custom method, which will output to both the console and to TestFairy sessions. One suggestion we have is to add a method that looks like this:

Code Example

// Be sure to import TestFairy
using TestFairyLib;

public static void Log(string format, params object[] arg)
{
    using (var nsFormat = new NSString(string.Format(format, arg)))
    {
        CFunctions.TFLog(nsFormat.Handle, "");
        Console.WriteLine(string.Format(format, arg));
    }
}
      

Now, you can log statements using this call:

Log("Hello {0}", "World");
            

Syntax

TestFairy.log("<message>");

Code Example

// Be sure to import TestFairy
using TestFairyUnity;

TestFairy.log("Hello, TestFairy!");
      

Syntax

AirTestFairy.log("<message>");

Code Example

// Be sure to import TestFairy
import com.testfairy.AirTestFairy;

AirTestFairy.log("Hello, TestFairy!");
      

Syntax

TiTestFairy.log("<message>");

Code Example

// Be sure to import TestFairy
var TiTestFairy = require('com.testfairy.titestfairy');

TiTestFairy.log("Hello, TestFairy!");
      

Last updated on 2023-03-23