简体   繁体   中英

Does pytest support unicode in test name?

Here is my test sample (test_time.py):

#!/usr/bin/python
# -*- coding: utf-8 -*-

import pytest
from datetime import datetime, timedelta

testdata = [
    (datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
    (datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
]

@pytest.mark.parametrize("a,b,expected", testdata, ids=[u"中文", u"English"])
def test_timedistance_v1(a, b, expected):
    diff = a - b
    assert diff != expected

Here is the pytest output:

============================================================================== FAILURES ==============================================================================
_________________________________________________________________ test_timedistance_v1[\u4e2d\u6587] _________________________________________________________________

a = datetime.datetime(2001, 12, 12, 0, 0), b = datetime.datetime(2001, 12, 11, 0, 0), expected = datetime.timedelta(1)

    @pytest.mark.parametrize("a,b,expected", testdata, ids=[u"中文", u"English"])
    def test_timedistance_v1(a, b, expected):
        diff = a - b
>       assert diff != expected
E       assert datetime.timedelta(1) != datetime.timedelta(1)

test_time.py:15: AssertionError
___________________________________________________________________ test_timedistance_v1[English] ____________________________________________________________________

a = datetime.datetime(2001, 12, 11, 0, 0), b = datetime.datetime(2001, 12, 12, 0, 0), expected = datetime.timedelta(-1)

    @pytest.mark.parametrize("a,b,expected", testdata, ids=[u"中文", u"English"])
    def test_timedistance_v1(a, b, expected):
        diff = a - b
>       assert diff != expected
E       assert datetime.timedelta(-1) != datetime.timedelta(-1)

test_time.py:15: AssertionError
====================================================================== 2 failed in 0.05 seconds ======================================================================

For the second line in output , the test name is "test_timedistance_v1[\中\文]" , I hope it's "test_timedistance_v1[中文]", does py.test support it? (my pytest version is 3.1.2, OS: macOS 10.12.5)

It does not depend of pytest but of your computer locale.

Here the trace-log of test ( LC_ALL="en_US.UTF-8" ) :

================================ test session starts ================================
platform linux -- Python 3.5.3, pytest-2.9.2, py-1.4.34, pluggy-0.3.1
rootdir: /home/..../tmp, inifile: 
collected 2 items 

pytest_chin.py FF

===================================== FAILURES ======================================
_____________________________ test_timedistance_v1[中文] ______________________________
...

And with with LC_ALL="fr_FR.iso8859-1" :

================================ test session starts ================================
platform linux -- Python 3.5.3, pytest-2.9.2, py-1.4.34, pluggy-0.3.1
rootdir: /home/gustavi/tmp, inifile: 
collected 2 items 

pytest_chin.py FF

===================================== FAILURES ======================================
\x1b[1m\x1b[31m_____________________________ test_timedistance_v1[\u4e2d\u6587] ______________________________\x1b[0m
...

Here an usefull link to setup your locale on OS X.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM