Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run script before running tests in cocoa unit test target

I'm currently testing an iOS app that communicates with an JSON API. I need to start a sinatra server before running the tests. The server works as a mock for the real API.

Is there any way to run a one line script like this one ruby /path/to/server.rb ?

Thanks

like image 381
bilby91 Avatar asked Dec 27 '25 15:12

bilby91


2 Answers

Go to "Mange Schemes" and select you scheme, then expand "Tests" and select "Pre-actions" and add a new run script:

enter image description here

Select "Provide build settings from:" enter image description here

I think the variable you are looking for is ${SRCROOT}

like image 93
Sebastian Avatar answered Dec 30 '25 03:12

Sebastian


In addition to @Sebastian answer make sure to add & after your ruby command since without it sinatra will block your tests execution.

Also it's useful to take care about post-action where you need to kill the ruby process. Following example uses bundler and rackup to start sinatra.

Example for pre-action script:

exec > /tmp/tests-pre-actions.log 2>&1
source ~/.bash_profile

SERVER_PATH="${PROJECT_DIR}"/"Server"
cd "$SERVER_PATH"
bundle exec rackup > /tmp/server.log 2>&1 &

#get the PID of the process
PID=$!

#save PID to file
echo $PID > /tmp/sinatra.pid

Example for post-action script:

exec > /tmp/tests-pre-actions.log 2>&1
source ~/.bash_profile

PID=$(</tmp/sinatra.pid)
echo "Sinatra server pid $PID"
kill -9 $PID

config.ru for rackup gem:

require './server'
trap('TERM') {Process.kill 'INT', Process.pid}
puts 'Run sinatra'
run Sinatra::Application
like image 28
SPopenko Avatar answered Dec 30 '25 03:12

SPopenko