Hello sekitaka.
If you are developing a Facebook app, you will need to implement tests for functions posted using the Graph API. At first I tested it with my own account, but when it came to automation I had to consider another way.
I found out that Facebook has the ability to create test users for development. This feature is provided both in the GUI and API, allowing you to create and delete test users. By using this to generate a Facebook test user for each test and execute the test, it is possible to support test automation.
The sample test case in python is as follows.
sample.py
# coding:utf-8
# !/usr/bin/python
import facebook
import unittest
from unittest import TestCase
#Function to be tested
#Just post a message on the wall
def post(message, fb_token):
graph = facebook.GraphAPI(fb_token)
graph.put_wall_post(message)
class Test(TestCase):
#Facebook test user
FB_USER = None
FB_APP_ID = 'YOUR_APP_ID'
FB_APP_SECRET = 'YOUR_APP_SECRET'
@classmethod
def setUpClass(cls):
super(Test, cls).setUpClass()
#Create test user
app_token = facebook.GraphAPI().get_app_access_token(cls.FB_APP_ID,
cls.FB_APP_SECRET,
True)
graph = facebook.GraphAPI(app_token)
cls.FB_USER = graph.request(cls.FB_APP_ID + '/accounts/test-users', {
'permissions': 'public_profile,user_photos,publish_actions'
}, {},
method='POST')
@classmethod
def tearDownClass(cls):
super(Test, cls).tearDownClass()
#Delete test user
app_token = facebook.GraphAPI().get_app_access_token(cls.FB_APP_ID,
cls.FB_APP_SECRET,
True)
graph = facebook.GraphAPI(app_token)
graph.request(cls.FB_USER['id'], {}, None, method='DELETE')
#Test that post can be posted normally
def test_post(self):
print Test.FB_USER
post("hogehoge", Test.FB_USER['access_token'])
if __name__ == '__main__':
unittest.main()
This time I put the sample code of python, but since Graph API is HTTP-based, I think that the same mechanism can be created regardless of the language. It is possible that the API specifications will change before you know it, so it's a good idea to perform regular tests on the CI of functions that use the Graph API.
Recommended Posts