1616import warnings
1717from collections import OrderedDict , defaultdict
1818
19- # for python 3 compatibility
20- import six
21-
22- try :
23- from Queue import Empty
24- except :
25- from queue import Empty
19+ from queue import Empty
2620
2721# for reading notebook files
2822import nbformat
@@ -71,9 +65,10 @@ def pytest_addoption(parser):
7165
7266 This is called by the pytest API
7367 """
74- group = parser .getgroup ("general" )
68+ group = parser .getgroup ("nbval" , "Jupyter Notebook validation" )
69+
7570 group .addoption ('--nbval' , action = 'store_true' ,
76- help = "Validate Jupyter notebooks" )
71+ help = "Run Jupyter notebooks, validating all output " )
7772
7873 group .addoption ('--nbval-lax' , action = 'store_true' ,
7974 help = "Run Jupyter notebooks, only validating output on "
@@ -86,8 +81,16 @@ def pytest_addoption(parser):
8681
8782 group .addoption ('--nbval-current-env' , action = 'store_true' ,
8883 help = 'Force test execution to use a python kernel in '
89- 'the same enviornment that py.test was '
90- 'launched from.' )
84+ 'the same environment that py.test was '
85+ 'launched from. Without this flag, the kernel stored '
86+ 'in the notebook is used by default. '
87+ 'See also: --nbval-kernel-name' )
88+
89+ group .addoption ('--nbval-kernel-name' , action = 'store' , default = None ,
90+ help = 'Force test execution to use the named kernel. '
91+ 'If a kernel is not named, the kernel stored in the '
92+ 'notebook is used by default. '
93+ 'See also: --current-env' )
9194
9295 group .addoption ('--nbval-cell-timeout' , action = 'store' , default = 2000 ,
9396 type = float ,
@@ -124,6 +127,11 @@ def pytest_configure(config):
124127 if config .option .nbval_current_env :
125128 raise ValueError ("--current-env and --nbval-current-env were both supplied." )
126129 config .option .nbval_current_env = config .option .current_env
130+ if config .option .nbval or config .option .nbval_lax :
131+ if config .option .nbval_kernel_name and config .option .current_env :
132+ raise ValueError ("--current-env and --nbval-kernel-name are mutually exclusive." )
133+
134+
127135
128136
129137def pytest_collect_file (path , parent ):
@@ -248,9 +256,12 @@ def setup(self):
248256 Called by pytest to setup the collector cells in .
249257 Here we start a kernel and setup the sanitize patterns.
250258 """
251-
259+ # we've already checked that --nbval-current-env and
260+ # --nbval-kernel-name were not both supplied
252261 if self .parent .config .option .nbval_current_env :
253262 kernel_name = CURRENT_ENV_KERNEL_NAME
263+ elif self .parent .config .option .nbval_kernel_name :
264+ kernel_name = self .parent .config .option .nbval_kernel_name
254265 else :
255266 kernel_name = self .nb .metadata .get (
256267 'kernelspec' , {}).get ('name' , 'python' )
@@ -519,9 +530,9 @@ def compare_outputs(self, test, ref, skip_compare=None):
519530
520531 def format_output_compare (self , key , left , right ):
521532 """Format an output for printing"""
522- if isinstance (left , six . string_types ):
533+ if isinstance (left , str ):
523534 left = _trim_base64 (left )
524- if isinstance (right , six . string_types ):
535+ if isinstance (right , str ):
525536 right = _trim_base64 (right )
526537
527538 cc = self .colors
@@ -815,7 +826,7 @@ def sanitize_outputs(self, outputs, skip_sanitize=('metadata',
815826 def sanitize (self , s ):
816827 """sanitize a string for comparison.
817828 """
818- if not isinstance (s , six . string_types ):
829+ if not isinstance (s , str ):
819830 return s
820831
821832 """
@@ -824,7 +835,7 @@ def sanitize(self, s):
824835 is passed when py.test is called. Otherwise, the strings
825836 are not processed
826837 """
827- for regex , replace in six . iteritems ( self .parent .sanitize_patterns ):
838+ for regex , replace in self .parent .sanitize_patterns . items ( ):
828839 s = re .sub (regex , replace , s )
829840 return s
830841
@@ -920,6 +931,6 @@ def _trim_base64(s):
920931
921932def _indent (s , indent = ' ' ):
922933 """Intent each line with indent"""
923- if isinstance (s , six . string_types ):
934+ if isinstance (s , str ):
924935 return '\n ' .join (('%s%s' % (indent , line ) for line in s .splitlines ()))
925936 return s
0 commit comments