Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

Why is my unit test case missing the if statements to cover in python?

I have written a test case for a function. The function basically returns an output whose content type is a text/csv. Upon running the command python3 -m pytest --cov-report term-missing --cov=projfolder/ I get the lines which are being missed and those are all in the if statements. My understanding of the issue is that the testcase passes but misses the statements which are in the if loop. Look at this example func.py

@process_bp.route("/getListCSV")
def get_list_csv():

    companies_flask_list = db.session.query(Companies, LeadsStatus).join(LeadsStatus,
                                                                         Companies.id == LeadsStatus.company_id)

    industry_sele = "All"
    country_sele = "UK"
    size_sele = "All"

    filters = {'open': True, 'assign': True, 'closed': True}

    status_list = ['open', 'allocated', 'converted', 'not converted']

    if 'industry' in request.form:
        industry_sele = request.form['industry']
        country_sele = request.form['country']
        size_sele = request.form['size']

        if not request.form.get('open'):
            filters['open'] = False
            if 'open' in status_list:
                status_list.remove('open')
        if not request.form.get('assign'):
            filters['assign'] = False
            if 'allocated' in status_list:
                status_list.remove('allocated')
        if not request.form.get('closed'):
            filters['closed'] = False
            if 'converted' in status_list:
                status_list.remove('converted')
            if 'not converted' in status_list:
                status_list.remove('not converted')

    if 'industry' in request.args:
        industry_sele = request.args.get('industry')
        country_sele = request.args.get('country')
        size_sele = request.args.get('size')

        if request.args.get('open') == 'False':
            filters['open'] = False
            if 'open' in status_list:
                status_list.remove('open')

        if request.args.get('assign') == 'False':
            filters['assign'] = False

            if 'allocated' in status_list:
                status_list.remove('allocated')

        if request.args.get('closed') == 'False':
            filters['closed'] = False
            if 'converted' in status_list:
                status_list.remove('converted')
            if 'not converted' in status_list:
                status_list.remove('not converted')

All these lines (which are inside each if statements are missing). Look at my way of unit testing this function. test_func.py

    def test_get_list_csv_manager(self):
        with self.client.session_transaction() as session:
            session['role'] = 'manager'
            session['id'] = 2
        res = self.client.get("/getListCSV", follow_redirects=True)
        self.assertEqual(res.status_code, 200)

How should I write the testcase so that these if statements would be covered in these? I am stuck like a bee on this issue since 5days and I have no help or luck whatsoever.

EDIT: Here is how I tried it the other way around.

    def test_get_list_csv_form_requests(self):
        with self.client.session_transaction() as session:
            session['role'] = 'manager'
            session['id'] = 2

            mock_request_data = {
                'industry_sele': 'Compliance',
                'country_sele': 'UK',
                'size_sele': '5000'
            }
        res = self.client.get("/getListCSV", data=json.dumps(mock_request_data),follow_redirects=True)
        self.assertEqual(res.status_code, 200)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally Found an answer to this question of mine. What worked for me was this piece of code.

    def test_request_form_csv(self):
        with self.client.session_transaction() as session:
            session['role'] = 'manager'
            session['id'] = 2
        r = self.client.get('/getListCSV', data={'industry': 'All', 'country': 'UK', 'size': 'All'})
        self.assertEqual(r.status_code, 200)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...