#_*_coding:utf-8_*_
import unittest,os,ddt,json
from globalVar import gl
from library import HTMLTESTRunnerCN

from library import scripts
from library.http import HttpWebRequest

@ddt.ddt
class TestProduct(unittest.TestCase):
    """Product模块"""
    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass


    @ddt.data(*(scripts.loadDdtData(filename='Product.yaml',caseflag='PRODUCT_CASE1')))
    def testProductlist(self,data):
        '''菜品接口:商家点菜记录列表/product/list'''

        # 整合数据，调用接口，获取返回结果
        res = scripts.loadtestInterface(
            instance=HttpWebRequest(),
            instance_pro='post',
            data=data['ProductList'],
            appid=data['Appid'],
            desc=data['Desc'],
            url=data['Url'],
            Appkey=data['Appkey']
        )
        #断言
        self.assertEqual(res['errcode'],0,res['errmsg'])
        #self.assertTrue(res['res'])

    @ddt.data(*(scripts.loadDdtData(filename='Product.yaml',caseflag='PRODUCT_CASE2')))
    def testProductlistbyuser(self,data):
        '''菜品接口:查询会员点菜记录/product/listbyuser'''

        # 整合数据，调用接口，获取返回结果
        res = scripts.loadtestInterface(
            instance=HttpWebRequest(),
            instance_pro='post',
            data=data['ProductListByUser'],
            appid=data['Appid'],
            desc=data['Desc'],
            url=data['Url']
        )
        # 断言
        self.assertEqual(res['errcode'], 0, res['errmsg'])
        #self.assertTrue(res['res'])



if __name__=="__main__":

    suite = unittest.TestSuite()
    tests = [unittest.TestLoader().loadTestsFromTestCase(TestProduct)]
    suite.addTests(tests)

    filePath = os.path.join(gl.reportPath, 'Report.html')  # 确定生成报告的路径
    print filePath

    with file(filePath, 'wb') as fp:
        runner = HTMLTESTRunnerCN.HTMLTestRunner(
            stream=fp,
            title=u'接口自动化测试报告',
            description=u'详细测试用例结果',  # 不传默认为空
            tester=u"yhleng"  # 测试人员名字，不传默认为小强
        )
        # 运行测试用例
        runner.run(suite)
        fp.close()