[PYTHON] Automate Facebook App Testing with Facebook Test Users

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.

Graph API test automation flow

  1. Create a Facebook test user at the start of the test
  2. Unit test
  3. Delete Facebook test user

sample

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()

Summary

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

Automate Facebook App Testing with Facebook Test Users
Automate smartphone app testing with Appium-Python
Automate python testing with CircleCI
Automate Windows Application Testing with Windows Application Driver-Python
Automate UI testing with Selenium API | Crawling websites with python
[Golang] Test the function error termination "os.Exit (1)" with testing
[Blender] Automate Blender add-on testing with GitHub and Travis CI
Primality test with Python
Strengthen with code test ⑨
Strengthen with code test ③
Strengthen with code test ⑤
Strengthen with code test ④
Primality test with python
linux with termux app
Strengthen with code test ②
Testing Elasticsearch with python-tcptest
Strengthen with code test ①
Strengthen with code test ⑧
Strengthen with code test ⑨