I am trying to learn pytest and testing my knowledge on the below code.
src.py
def a(s):
if s == 1:
return True
if s == 2:
return False
return 3
def abc(u):
if u ==1:
if a(u):
return 1
else:
if a(u):
return 2
else:
return 3
and this is my test file:
import pytest
import src
#pytest.mark.parametrize("input_data, expected", [(1,1), (2,2), (3,2)])
def test_abc(input_data, expected, mocker):
s = mocker.patch('src.a', side_effect=[True, False])
assert src.abc(input_data) == expected
s.assert_called_once_with(input_data)
#pytest.mark.parametrize("input_data, expected", [(1,True), (2,False), (3,3)])
def test_a(input_data, expected,):
assert src.a(input_data) == expected
Testing the code returns all passed, but the coverage reports that the line 16 is not being tested:
% pytest -v --cov=. . --cov-report term-missing
==================================================================== test session starts ====================================================================
platform darwin -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0 -- /Users/tomhanks/.pyenv/versions/3.10.4/bin/python3.10
cachedir: .pytest_cache
rootdir: /Users/tomhanks/projects/pytest
plugins: mock-3.7.0, cov-3.0.0
collected 6 items
test_me.py::test_abc[1-1] PASSED [ 16%]
test_me.py::test_abc[2-2] PASSED [ 33%]
test_me.py::test_abc[3-2] PASSED [ 50%]
test_me.py::test_a[1-True] PASSED [ 66%]
test_me.py::test_a[2-False] PASSED [ 83%]
test_me.py::test_a[3-3] PASSED [100%]
---------- coverage: platform darwin, python 3.10.4-final-0 ----------
Name Stmts Miss Cover Missing
------------------------------------------
src.py 13 1 92% 16
test_me.py 10 0 100%
------------------------------------------
TOTAL 23 1 96%
===================================================================== 6 passed in 0.07s =====================================================================
Can somebody please help me understand why the line 16 is not being tested?
Thanks in advance!
Related
I have a simple program for traslating morse code back and forth as a school project and part of it is also writting unit tests. I ve tried for multiple hours but was not succesful at all
dictionary={'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..'}
dictionary2={'.-':'A','-...':'B','-.-.':'C','-..':'D', '.':'E','..-.':'F','--.':'G','....':'H',
'..':'I','.---':'J', '-.-':'K','.-..':'L', '--':'M', '-.':'N',
'---':'O', '.--.':'P', '--.-':'Q','.-.':'R', '...':'S', '-':'T',
'..-':'U', '...-':'V', '.--':'W','-..-':'X', '-.--':'Y', '--..':'Z', '':' '}
def coding(s):
void=""
for i in s: #indexing
if i != ' ':
void+=dictionary[i]+' '
else:
void +=' '
print(void)
def decoding(s):
void = ""
splitstring=s.split(" ")
splitstring = s.split(" ")#splitting by spaces
#splitstring=splitstring.remove('')
for i in splitstring: #indexing
void += dictionary2[i]
print(void)
def selection(f):
f=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
return f
c = 1
d = 0
while(c!="0"):
d =selection(d)
a=input("ZADEJ TEXT NA ŠIFROVÁNÍ: ")
a=a.upper()
startUp="" # not being used but program cant be ran without it for some reason
if d==1: # translating to morse code
coding(a)
else: # translating from morse code
decoding(a)
c = input(("POKUD CHCTE PROGRAM UKONČIT, ZADEJTE 0 || PRO POKRAČOVÁNÍ ZADEJTE LIBOVOLNÝ ZNAK : "))
this is what i ve tried:
import pytest
from example import coding
from example import decoding
def test_coding():
assert coding('HELLO') == '.... . .-.. .-.. --- '
assert coding('WORLD') == '.-- .-. .. -. ..-. '
assert coding('HELLO WORLD') == '.... . .-.. .-.. --- .-- .-. .. -. ..-. '
test_coding()
#To write unit tests for the decoding() function, we can test that it decodes strings correctly by comparing the output of the function with the expected result. For example:
def test_decoding():
assert decoding('.... . .-.. .-.. --- ') == 'HELLO'
assert decoding('.-- .-. .. -. ..-. ') == 'WORLD'
assert decoding('.... . .-.. .-.. --- .-- .-. .. -. ..-. ') == 'HELLO WORLD'
test_decoding()
My teacher wants those tests to be ran with this command:
py.exe -m pytest --cov=. --cov-fail-under=66
in command block, it has to reach at least 66% success rate.
thus far i ve always ended up with this result:
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.9.6, pytest-7.2.0, pluggy-1.0.0
rootdir: C:\Users\ash31\OneDrive\Plocha\AP1VS-final-project
plugins: cov-4.0.0
collected 0 items / 1 error
================================================================================================================== ERRORS ==================================================================================================================
_____________________________________________________________________________________________________ ERROR collecting example_test.py _____________________________________________________________________________________________________
example_test.py:1: in <module>
from example import coding
example.py:51: in <module>
d =selection(d)
example.py:43: in selection
f=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\_pytest\capture.py:192: in read
raise OSError(
E OSError: pytest: reading from stdin while output is captured! Consider using `-s`.
------------------------------------------------------------------------------------------------------------- Captured stdout --------------------------------------------------------------------------------------------------------------
1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY
----------- coverage: platform win32, python 3.9.6-final-0 -----------
Name Stmts Miss Cover
-------------------------------------
example.py 32 22 31%
example_test.py 24 23 4%
-------------------------------------
TOTAL 56 45 20%
FAIL Required test coverage of 66% not reached. Total coverage: 19.64%
========================================================================================================= short test summary info ==========================================================================================================
ERROR example_test.py - OSError: pytest: reading from stdin while output is captured! Consider using `-s`.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================= 1 error in 0.46s =============================================================================================================
New at this so please bare with me. Following these instructions trying update to the latest version of Python
https://www.vultr.com/docs/update-python3-on-centos/
I get all the way to step 2.5 and get the following:
[user1#localhost Python-3.9.6]$ ./configure --enable-optimizations
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.9... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/home/user1/Python-3.9.6':
configure: error: C compiler cannot create executables
See `config.log' for more details
My config.log file
## --------- ##
## Platform. ##
## --------- ##
hostname = localhost.localdomain
uname -m = x86_64
uname -r = 3.10.0-1160.76.1.el7.x86_64
uname -s = Linux
uname -v = #1 SMP Wed Aug 10 16:21:17 UTC 2022
/usr/bin/uname -p = x86_64
/bin/uname -X = unknown
/bin/arch = x86_64
/usr/bin/arch -k = unknown
/usr/convex/getsysinfo = unknown
/usr/bin/hostinfo = unknown
/bin/machine = unknown
/usr/bin/oslevel = unknown
/bin/universe = unknown
PATH: /opt/rh/devtoolset-8/root/usr/bin
PATH: /usr/local/bin
PATH: /usr/bin
PATH: /usr/local/sbin
PATH: /usr/sbin
PATH: /home/user1/.local/bin
PATH: /home/user1/bin
## ----------- ##
## Core tests. ##
## ----------- ##
configure:2848: checking build system type
configure:2862: result: x86_64-pc-linux-gnu
configure:2882: checking host system type
configure:2895: result: x86_64-pc-linux-gnu
configure:2925: checking for python3.9
configure:2955: result: no
configure:2925: checking for python3
configure:2941: found /usr/bin/python3
configure:2952: result: python3
configure:3046: checking for --enable-universalsdk
configure:3093: result: no
configure:3117: checking for --with-universal-archs
configure:3132: result: no
configure:3288: checking MACHDEP
configure:3339: result: "linux"
configure:3633: checking for gcc
configure:3649: found /opt/rh/devtoolset-8/root/usr/bin/gcc
configure:3660: result: gcc
configure:3889: checking for C compiler version
configure:3898: gcc --version >&5
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
configure:3909: $? = 0
configure:3898: gcc -v >&5
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.3.1-20190311/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
configure:3909: $? = 0
configure:3898: gcc -V >&5
gcc: error: unrecognized command line option '-V'
gcc: fatal error: no input files
compilation terminated.
configure:3909: $? = 1
configure:3898: gcc -qversion >&5
gcc: error: unrecognized command line option '-qversion'; did you mean '--version'?
gcc: fatal error: no input files
compilation terminated.
configure:3909: $? = 1
configure:3929: checking whether the C compiler works
configure:3951: gcc conftest.c >&5
/lib/../lib64/crt1.o: file not recognized: File truncated
collect2: error: ld returned 1 exit status
configure:3955: $? = 1
configure:3993: result: no
configure: failed program was:
| /* confdefs.h */
| #define _GNU_SOURCE 1
| #define _NETBSD_SOURCE 1
| #define __BSD_VISIBLE 1
| #define _DARWIN_C_SOURCE 1
| #define _PYTHONFRAMEWORK ""
| #define _XOPEN_SOURCE 700
| #define _XOPEN_SOURCE_EXTENDED 1
| #define _POSIX_C_SOURCE 200809L
| /* end confdefs.h. */
|
| int
| main ()
| {
|
| ;
| return 0;
| }
configure:3998: error: in `/home/user1/Python-3.9.6':
configure:4000: error: C compiler cannot create executables
See `config.log' for more details
## ---------------- ##
## Cache variables. ##
## ---------------- ##
ac_cv_build=x86_64-pc-linux-gnu
ac_cv_env_CC_set=
ac_cv_env_CC_value=
ac_cv_env_CFLAGS_set=
ac_cv_env_CFLAGS_value=
ac_cv_env_CPPFLAGS_set=
ac_cv_env_CPPFLAGS_value=
ac_cv_env_CPP_set=
ac_cv_env_CPP_value=
ac_cv_env_LDFLAGS_set=
ac_cv_env_LDFLAGS_value=
ac_cv_env_LIBS_set=
ac_cv_env_LIBS_value=
ac_cv_env_MACHDEP_set=
ac_cv_env_MACHDEP_value=
ac_cv_env_PKG_CONFIG_LIBDIR_set=
ac_cv_env_PKG_CONFIG_LIBDIR_value=
ac_cv_env_PKG_CONFIG_PATH_set=set
ac_cv_env_PKG_CONFIG_PATH_value=/opt/rh/devtoolset-8/root/usr/lib64/pkgconfig
ac_cv_env_PKG_CONFIG_set=
ac_cv_env_PKG_CONFIG_value=
ac_cv_env_PROFILE_TASK_set=
ac_cv_env_PROFILE_TASK_value=
ac_cv_env_build_alias_set=
ac_cv_env_build_alias_value=
ac_cv_env_host_alias_set=
ac_cv_env_host_alias_value=
ac_cv_env_target_alias_set=
ac_cv_env_target_alias_value=
ac_cv_host=x86_64-pc-linux-gnu
ac_cv_prog_PYTHON_FOR_REGEN=python3
ac_cv_prog_ac_ct_CC=gcc
## ----------------- ##
## Output variables. ##
## ----------------- ##
ABIFLAGS=''
ALT_SOABI=''
AR=''
ARCH_RUN_32BIT=''
ARFLAGS=''
BASECFLAGS=''
BASECPPFLAGS=''
BINLIBDEST=''
BLDLIBRARY=''
BLDSHARED=''
BUILDEXEEXT=''
CC='gcc'
CCSHARED=''
CFLAGS=''
CFLAGSFORSHARED=''
CFLAGS_ALIASING=''
CFLAGS_NODIST=''
CONFIGURE_MACOSX_DEPLOYMENT_TARGET=''
CONFIG_ARGS=' '\''--enable-optimizations'\'' '\''PKG_CONFIG_PATH=/opt/rh/devtoolset-8/root/usr/lib64/pkgconfig'\'''
CPP=''
CPPFLAGS=''
CXX=''
DEFS=''
DEF_MAKE_ALL_RULE=''
DEF_MAKE_RULE=''
DFLAGS=''
DLINCLDIR=''
DLLLIBRARY=''
DTRACE=''
DTRACE_HEADERS=''
DTRACE_OBJS=''
DYNLOADFILE=''
ECHO_C=''
ECHO_N='-n'
ECHO_T=''
EGREP=''
ENSUREPIP=''
EXEEXT=''
EXPORTSFROM=''
EXPORTSYMS=''
EXPORT_MACOSX_DEPLOYMENT_TARGET='#'
EXT_SUFFIX=''
FRAMEWORKALTINSTALLFIRST=''
FRAMEWORKALTINSTALLLAST=''
FRAMEWORKINSTALLAPPSPREFIX=''
FRAMEWORKINSTALLFIRST=''
FRAMEWORKINSTALLLAST=''
FRAMEWORKPYTHONW=''
FRAMEWORKUNIXTOOLSPREFIX='/usr/local'
GITBRANCH=''
GITTAG=''
GITVERSION=''
GNULD=''
GREP=''
HAS_GIT='no-repository'
HAVE_GETHOSTBYNAME=''
HAVE_GETHOSTBYNAME_R=''
HAVE_GETHOSTBYNAME_R_3_ARG=''
HAVE_GETHOSTBYNAME_R_5_ARG=''
HAVE_GETHOSTBYNAME_R_6_ARG=''
INSTALL_DATA=''
INSTALL_PROGRAM=''
INSTALL_SCRIPT=''
INSTSONAME=''
LDCXXSHARED=''
LDFLAGS=''
LDFLAGS_NODIST=''
LDLIBRARY=''
LDLIBRARYDIR=''
LDSHARED=''
LDVERSION=''
LIBC=''
LIBFFI_INCLUDEDIR=''
LIBM=''
LIBOBJS=''
LIBPL=''
LIBPYTHON=''
LIBRARY=''
LIBS=''
LIBTOOL_CRUFT=''
LINKCC=''
LINKFORSHARED=''
LIPO_32BIT_FLAGS=''
LIPO_INTEL64_FLAGS=''
LLVM_AR=''
LLVM_AR_FOUND=''
LLVM_PROFDATA=''
LLVM_PROF_ERR=''
LLVM_PROF_FILE=''
LLVM_PROF_FOUND=''
LLVM_PROF_MERGER=''
LN=''
LTLIBOBJS=''
MACHDEP='linux'
MACHDEP_OBJS=''
MAINCC=''
MKDIR_P=''
MULTIARCH=''
MULTIARCH_CPPFLAGS=''
NO_AS_NEEDED=''
OBJEXT=''
OPENSSL_INCLUDES=''
OPENSSL_LDFLAGS=''
OPENSSL_LIBS=''
OPT=''
OTHER_LIBTOOL_OPT=''
PACKAGE_BUGREPORT='https://bugs.python.org/'
PACKAGE_NAME='python'
PACKAGE_STRING='python 3.9'
PACKAGE_TARNAME='python'
PACKAGE_URL=''
PACKAGE_VERSION='3.9'
PATH_SEPARATOR=':'
PGO_PROF_GEN_FLAG=''
PGO_PROF_USE_FLAG=''
PKG_CONFIG=''
PKG_CONFIG_LIBDIR=''
PKG_CONFIG_PATH='/opt/rh/devtoolset-8/root/usr/lib64/pkgconfig'
PLATFORM_TRIPLET=''
PLATLIBDIR=''
PROFILE_TASK=''
PY3LIBRARY=''
PYTHONFRAMEWORK=''
PYTHONFRAMEWORKDIR='no-framework'
PYTHONFRAMEWORKIDENTIFIER='org.python.python'
PYTHONFRAMEWORKINSTALLDIR=''
PYTHONFRAMEWORKPREFIX=''
PYTHON_FOR_BUILD='./$(BUILDPYTHON) -E'
PYTHON_FOR_REGEN='python3'
PY_ENABLE_SHARED=''
READELF=''
RUNSHARED=''
SED=''
SHELL='/bin/sh'
SHLIBS=''
SHLIB_SUFFIX=''
SOABI=''
SOVERSION='1.0'
SRCDIRS=''
TCLTK_INCLUDES=''
TCLTK_LIBS=''
THREADHEADERS=''
TRUE=''
TZPATH=''
UNIVERSALSDK=''
UNIVERSAL_ARCH_FLAGS=''
VERSION='3.9'
_PYTHON_HOST_PLATFORM=''
ac_ct_AR=''
ac_ct_CC='gcc'
ac_ct_CXX=''
ac_ct_READELF=''
bindir='${exec_prefix}/bin'
build='x86_64-pc-linux-gnu'
build_alias=''
build_cpu='x86_64'
build_os='linux-gnu'
build_vendor='pc'
datadir='${datarootdir}'
datarootdir='${prefix}/share'
docdir='${datarootdir}/doc/${PACKAGE_TARNAME}'
dvidir='${docdir}'
exec_prefix='NONE'
host='x86_64-pc-linux-gnu'
host_alias=''
host_cpu='x86_64'
host_os='linux-gnu'
host_vendor='pc'
htmldir='${docdir}'
includedir='${prefix}/include'
infodir='${datarootdir}/info'
libdir='${exec_prefix}/lib'
libexecdir='${exec_prefix}/libexec'
localedir='${datarootdir}/locale'
localstatedir='${prefix}/var'
mandir='${datarootdir}/man'
oldincludedir='/usr/include'
pdfdir='${docdir}'
prefix='NONE'
program_transform_name='s,x,x,'
psdir='${docdir}'
sbindir='${exec_prefix}/sbin'
sharedstatedir='${prefix}/com'
sysconfdir='${prefix}/etc'
target_alias=''
## ----------- ##
## confdefs.h. ##
## ----------- ##
/* confdefs.h */
#define _GNU_SOURCE 1
#define _NETBSD_SOURCE 1
#define __BSD_VISIBLE 1
#define _DARWIN_C_SOURCE 1
#define _PYTHONFRAMEWORK ""
#define _XOPEN_SOURCE 700
#define _XOPEN_SOURCE_EXTENDED 1
#define _POSIX_C_SOURCE 200809L
configure: exit 77
Is there something that I need to install within my Linux (Centos 7) to get this to work?
My current Python Version is 3.6.8
Trying to do test files in PyCharm with pytest and I repeatedly get the "fixture [variable name] not found. All that I could find regarding this issue are cases of misspelling parametrize.
liste_paie = []
def calculer_paie_employe(tauxh,heures):
total = tauxh * heures
impot = total * 0.20
net = total - impot
liste_paie = [heures, tauxh, total, impot, net]
return liste_paie
pytest.mark.parametrize("var1,var2,expected_1,expected_2,expected_3", [(14.7 , 25,367.5,73.5,294), (20 , 15, 300, 60, 240),
(15.6 , 23.9, 372.84, 75.568, 300)])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
calculer_paie_employe(var1,var2)
assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
When I run it I get:
test setup failed
E fixture 'var1' not found
available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
use 'pytest --fixtures [testpath]' for help on them.
Final set of data should fail to pass. (this is intentional)
You must use it as a decorator, i.e. use the # syntax:
liste_paie = []
def calculer_paie_employe(tauxh,heures):
total = tauxh * heures
impot = total * 0.20
net = total - impot
liste_paie = [heures, tauxh, total, impot, net]
return liste_paie
import pytest
#pytest.mark.parametrize(
"var1,var2,expected_1,expected_2,expected_3", [
(14.7, 25, 367.5, 73.5, 294),
(20, 15, 300, 60, 240),
(15.6, 23.9, 372.84, 75.568, 300)
])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
liste_paie = calculer_paie_employe(var1,var2)
assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
a pytest run will then produce:
================================================= test session starts =================================================
platform win32 -- Python 3.5.4, pytest-3.10.1, py-1.8.0, pluggy-0.9.0
rootdir: c:\srv\tmp, inifile:
plugins: django-3.10.0, cov-2.6.1
collected 3 items
pytestparm.py ..F [100%]
====================================================== FAILURES =======================================================
_______________________________ test_calculer_paie_employe[15.6-23.9-372.84-75.568-300] _______________________________
var1 = 15.6, var2 = 23.9, expected_1 = 372.84, expected_2 = 75.568, expected_3 = 300
#pytest.mark.parametrize(
"var1,var2,expected_1,expected_2,expected_3", [
(14.7, 25, 367.5, 73.5, 294),
(20, 15, 300, 60, 240),
(15.6, 23.9, 372.84, 75.568, 300)
])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
liste_paie = calculer_paie_employe(var1,var2)
> assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
E assert (372.84 == 372.84 and 74.568 == 75.568)
pytestparm.py:19: AssertionError
========================================= 1 failed, 2 passed in 0.04 seconds ==========================================
Note that I've changed the code to use the return value, since the assignment to liste_paie in calculer_paie_employe doesn't change the global variable (because you're missing the global keyword - but using the return value is better practice anyways...)
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[\u4e2d\u6587]" , 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.
I have a project with many subdirectories and types of files (python, c++, configuration files, images, etc).
When I use SCons env.Package like this:
env.Package(
NAME = 'isid',
VERSION = '1.0',
PACKAGEVERSION = '11',
PACKAGETYPE = 'rpm',
LICENSE = 'gpl',
SUMMARY = 'just a test',
DESCRIPTION = 'the most test app in the world',
X_RPM_GROUP = 'Application/isid',
SOURCE_URL = 'http://isid.com/versions/isid-1.0.11.tar.gz',
)
I get everything in isid-1.0.11.tar.gz except for the h files.
This automatically leads to build errors in ./isid-1.0.11 that stops rpmbuild from running.
EDIT
My project is split into few subdirectories.
In each I have SConscript that starts with these lines, or similar, depending on the includes it needs:
# import all variables
Import('*')
include = Dir([
'../inc/',
])
local_env = env.Clone( CPPPATH = include )
SConstruct just defines the variables and calls SConscript() on each subdirectory.
The call to Package is done in SConstruct, so I guess SCons indeed does not know the dependencies.
snippet of
# scons --tree=prune:
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `.' is up to date.
+-.
+-SConstruct
+-correlator
| +-correlator/SConscript
| +-correlator/collector
| +-correlator/correlation_command
| | +-correlator/correlation_command/app_command_device.cpp
| | +-correlator/correlation_command/app_command_device.h
| | +-correlator/correlation_command/app_command_device.o
| | | +-correlator/correlation_command/app_command_device.cpp
| | | +-correlator/correlation_command/app_command_device.h
| | | +-correlator/entity/entity.h
| | | +-infra/inc/app_command/app_command.h
| | | +-/bin/g++
| | +-correlator/correlation_command/app_command_event.cpp
| | +-correlator/correlation_command/app_command_event.h
EDIT #2
Here is a complete, minimal, example that produces the same problem.
To reproduce the problem, run:
scons pack=1.0 ver=1
files:
SConstruct.
app1/SConscript
app1/main.cpp
app1/inc.h
Listing
SConstruct
1
2 # main file
3
4 env = Environment(tools=['default', 'packaging'])
5
6 Export( 'env' )
7
8 flags = [
9 '-Wall',
10 '-Werror',
11 '-g',
12 '-ggdb3',
13 '-gdwarf-2',
14 '-std=c++11',
15 ]
16
17 env.Append( CPPFLAGS = flags )
18
19
20 scripts = []
21
22 Sapp1 = 'app1/SConscript'
23 scripts.append( Sapp1 )
24
25
26 env.SConscript( scripts )
27
28 pack = ARGUMENTS.get('pack', '')
29 ver = ARGUMENTS.get('ver', '99' )
30 if pack:
31 env.Package(
32 NAME = 'app1',
33 VERSION = pack,
34 PACKAGEVERSION = ver,
35 PACKAGETYPE = 'rpm',
36 LICENSE = 'private',
37 SUMMARY = 'exampe app #1',
38 DESCRIPTION = 'the most powerfull exampe #1',
39 X_RPM_GROUP = 'Application/app1',
40 SOURCE_URL = 'http://example.com/1/app1-1.0.1.tar.gz',
41 )
42
app1/SConscript
1
2 # import all variables
3 Import('*')
4
5 # add specific include directory
6
7 include = Dir( [
8 '.',
9 ])
10
11 local_env = env.Clone( CPPPATH = include )
12
13 # define sources
14 sources = [
15 'main.cpp',
16 ]
17
18 libs = [
19 ]
20
21 main_name = 'app1',
22
23 main_obj = local_env.Program( target = main_name, source = sources, LIBS = libs )
24
25 # install
26 install_dir = '/opt/rf/app1'
27 install_files = [ main_obj ]
28
29 local_env.Install( dir = install_dir, source = install_files )
30 local_env.Command( install_dir, install_files, "chown -R rf:rfids $TARGET" )
31
32
33 local_env.Alias( 'install', install_dir )
app1/main.cpp
1
2 #include "inc.h"
3
4
5 int main()
6 {
7 int l = g;
8
9 return l;
10 }
app1/inc.h
1
2 int g = 100;
output:
# scons pack=1.0 ver=1
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o app1/main.o -c -Wall -Werror -g -ggdb3 -gdwarf-2 -std=c++11 -Iapp1 app1/main.cpp
g++ -o app1/app1 app1/main.o
LC_ALL=C rpmbuild -ta /home/ran/work/rpmexample/app1-1.0.1.tar.gz
scons: *** [app1-1.0-1.src.rpm] app1-1.0-1.src.rpm: Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.wU9lDZ
+ umask 022
+ cd /home/ran/work/rpmexample/rpmtemp/BUILD
+ '[' -n /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64 -a /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64 '!=' / ']'
+ rm -rf /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64
+ cd /home/ran/work/rpmexample/rpmtemp/BUILD
+ rm -rf app1-1.0
+ /usr/bin/gzip -dc /home/ran/work/rpmexample/app1-1.0.1.tar.gz
+ /usr/bin/tar -xf -
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd app1-1.0
+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.gVaX4j
+ umask 022
+ cd /home/ran/work/rpmexample/rpmtemp/BUILD
+ cd app1-1.0
+ '[' '!' -e /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64 -a /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64 '!=' / ']'
+ mkdir /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.JWAdxE
+ umask 022
+ cd /home/ran/work/rpmexample/rpmtemp/BUILD
+ '[' /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64 '!=' / ']'
+ rm -rf /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64
++ dirname /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64
+ mkdir -p /home/ran/work/rpmexample/rpmtemp/BUILDROOT
+ mkdir /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64
+ cd app1-1.0
+ scons --install-sandbox=/home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64 /home/ran/work/rpmexample/rpmtemp/BUILDROOT/app1-1.0-1.x86_64
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o app1/main.o -c -Wall -Werror -g -ggdb3 -gdwarf-2 -std=c++11 -Iapp1 app1/main.cpp
app1/main.cpp:2:17: fatal error: inc.h: No such file or directory
#include "inc.h"
^
compilation terminated.
scons: *** [app1/main.o] Error 1
scons: building terminated because of errors.
error: Bad exit status from /var/tmp/rpm-tmp.JWAdxE (%install)
RPM build errors:
Bad exit status from /var/tmp/rpm-tmp.JWAdxE (%install)
scons: building terminated because of errors.