def setUpClass(cls): # start Chrome options = webdriver.ChromeOptions() options.add_argument('headless') try: cls.client = webdriver.Chrome(chrome_options=options) except: pass # skip these tests if the browser could not be started if cls.client: # create the application cls.app = create_app('testing') cls.app_context = cls.app.app_context() cls.app_context.push() # suppress logging to keep unittest output clean import logging logger = logging.getLogger('werkzeug') logger.setLevel("ERROR") # create the database and populate with some fake data db.create_all() Role.insert_roles() fake.users(10) fake.posts(10) # add an administrator user admin_role = Role.query.filter_by(name='Administrator').first() admin = User(email='*****@*****.**', username='******', password='******', role=admin_role, confirmed=True) db.session.add(admin) db.session.commit() # start the Flask server in a thread cls.server_thread = threading.Thread(target=cls.app.run, kwargs={'debug': False}) cls.server_thread.start() # give the server a second to ensure it is up time.sleep(1)
def setUpClass(cls): # 启动浏览器 # options = webdriver.ChromeOption() # options.add_argument('headless') try: cls.client = webdriver.Chrome('C:/Python/chromedriver') except: pass # 如果无法启动浏览器 则跳过这些测试 if cls.client: cls.app = create_app('testing') cls.app_context = cls.app.app_context() cls.app_context.push() # 禁止日志,保持输出简洁 import logging logger = logging.getLogger('werkzeug') logger.setLevel('ERROR') # 创建数据库, 使用一些虚拟数据填充 db.create_all() Role.insert_roles() # User.generate_fake(10) # Post.generate_fake(10) fake.users(10) fake.posts(10) #添加管理员 admin_role = Role.query.filter_by(permissions=0xff).first() admin = User(email='*****@*****.**', username = '******', password='******', role = admin_role, confirmed=True) db.session.add(admin) db.session.commit() # 在一个线程中启动Flask服务器 cls.server_thread = threading.Thread(target=cls.app.run, kwargs={'debug':False}) cls.server_thread.start() time.sleep(1)
def setUpClass(cls): # 启动Chrome options = webdriver.ChromeOptions() # options.add_argument('headless') # 用谷歌浏览器的无头模式(没有界面) try: cls.client = webdriver.Chrome(chrome_options=options) except: pass # 如果无法启动浏览器则跳过这些测试 if cls.client: # 创建程序 cls.app = create_app('testing') cls.app_context = cls.app.app_context() cls.app_context.push() # 禁止日志,保持输出简洁 import logging logger = logging.getLogger('werkzeug') logger.setLevel('ERROR') # 创建数据库,并生成一些虚拟数据 # db.drop_all() # 如果有脏数据,先清空 # db.session.remove() db.create_all() Role.insert_roles() fake.users(10) fake.posts(10) # 添加管理员 admin_role = Role.query.filter_by(name='Administrator').first() admin = User(email='*****@*****.**', username='******', password='******', role=admin_role, confirmed=True) db.session.add(admin) db.session.commit() # 在一个线程中启动Flask服务起 threading.Thread(target=cls.app.run).start()
def generate_fake(): users(50) posts(50)
def fake_data(): fake.users() fake.posts(200)
def run_fake(): from app import fake fake.users() fake.posts()
def test_posts(self): # add a user r = Role.query.filter_by(name='User').first() self.assertIsNotNone(r) u = User(email='*****@*****.**', password='******', confirmed=True, role=r) db.session.add(u) db.session.commit() # write a post response = self.client.post( '/api/v1/posts/', headers=self.get_api_headers('*****@*****.**', 'cat'), data=json.dumps({'body': 'body of the *blog* post'})) self.assertEqual(response.status_code, 201) url = response.headers.get('location') self.assertIsNotNone(url) # get the new post response = self.client.get(url, headers=self.get_api_headers( '*****@*****.**', 'cat')) self.assertEqual(response.status_code, 200) json_response = json.loads(response.get_data(as_text=True)) self.assertEqual('http://localhost' + json_response['url'], url) self.assertEqual(json_response['body'], 'body of the *blog* post') self.assertEqual(json_response['body_html'], '<p>body of the <em>blog</em> post</p>') # edit the new post response = self.client.put( url, headers=self.get_api_headers('*****@*****.**', 'cat'), data=json.dumps({'body': 'body of *changed* post'})) self.assertEqual(response.status_code, 200) json_response = json.loads(response.get_data(as_text=True)) self.assertIsNotNone(url) self.assertEqual(json_response['body'], 'body of *changed* post') self.assertEqual(json_response['body_html'], '<p>body of <em>changed</em> post</p>') # post count, posts exist, next posts(count=25) response = self.client.get('/api/v1/posts/', headers=self.get_api_headers( '*****@*****.**', 'cat')) self.assertEqual(response.status_code, 200) json_response = json.loads(response.get_data(as_text=True)) self.assertEqual(json_response['count'], 26) self.assertEqual(json_response['next_url'], '/api/v1/posts/?page=2') self.assertIsNotNone(json_response['posts']) self.assertEqual(json_response['prev_url'], None) next_url = json_response['next_url'] # use next page button response = self.client.get(next_url, headers=self.get_api_headers( '*****@*****.**', 'cat')) self.assertEqual(response.status_code, 200) json_response = json.loads(response.get_data(as_text=True)) self.assertEqual(json_response['count'], 26) self.assertEqual(json_response['next_url'], None) self.assertIsNotNone(json_response['posts']) self.assertEqual(json_response['prev_url'], '/api/v1/posts/?page=1') prev_url = json_response['prev_url'] edit_url = json_response['posts'][0][ 'url'] #for edit authentication failed # use previous page button response = self.client.get(prev_url, headers=self.get_api_headers( '*****@*****.**', 'cat')) self.assertEqual(response.status_code, 200) json_response = json.loads(response.get_data(as_text=True)) self.assertEqual(json_response['next_url'], '/api/v1/posts/?page=2') # test edit authentication failed ###################################################### # add user that didn't exist when any post was randomly created r = Role.query.filter_by(name='User').first() self.assertIsNotNone(r) u = User(email='*****@*****.**', password='******', confirmed=True, role=r) db.session.add(u) db.session.commit() ###################################################### response = self.client.put( edit_url, headers=self.get_api_headers('*****@*****.**', 'cat'), data=json.dumps({'body': 'failed to edit another\'s post'})) self.assertEqual(response.status_code, 403) json_response = json.loads(response.get_data(as_text=True))
def make_fixtures(): from app.fake import posts, users users(count=20) posts()
def generate_fake_posts(number): posts(int(number))