conftest.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. """
  2. Testing
  3. =======
  4. General guidelines for writing good tests:
  5. - doctests always assume ``import networkx as nx`` so don't add that
  6. - prefer pytest fixtures over classes with setup methods.
  7. - use the ``@pytest.mark.parametrize`` decorator
  8. - use ``pytest.importorskip`` for numpy, scipy, pandas, and matplotlib b/c of PyPy.
  9. and add the module to the relevant entries below.
  10. """
  11. import sys
  12. import warnings
  13. import pytest
  14. import networkx
  15. def pytest_addoption(parser):
  16. parser.addoption(
  17. "--runslow", action="store_true", default=False, help="run slow tests"
  18. )
  19. def pytest_configure(config):
  20. config.addinivalue_line("markers", "slow: mark test as slow to run")
  21. def pytest_collection_modifyitems(config, items):
  22. # Allow pluggable backends to add markers to tests when
  23. # running in auto-conversion test mode
  24. networkx.classes.backends._mark_tests(items)
  25. if config.getoption("--runslow"):
  26. # --runslow given in cli: do not skip slow tests
  27. return
  28. skip_slow = pytest.mark.skip(reason="need --runslow option to run")
  29. for item in items:
  30. if "slow" in item.keywords:
  31. item.add_marker(skip_slow)
  32. # TODO: The warnings below need to be dealt with, but for now we silence them.
  33. @pytest.fixture(autouse=True)
  34. def set_warnings():
  35. warnings.filterwarnings(
  36. "ignore",
  37. category=DeprecationWarning,
  38. message="literal_stringizer is deprecated",
  39. )
  40. warnings.filterwarnings(
  41. "ignore",
  42. category=DeprecationWarning,
  43. message="literal_destringizer is deprecated",
  44. )
  45. # create_using for scale_free_graph
  46. warnings.filterwarnings(
  47. "ignore", category=DeprecationWarning, message="The create_using argument"
  48. )
  49. warnings.filterwarnings(
  50. "ignore", category=DeprecationWarning, message="nx.nx_pydot"
  51. )
  52. warnings.filterwarnings(
  53. "ignore",
  54. category=DeprecationWarning,
  55. message="\n\nThe `attrs` keyword argument of node_link",
  56. )
  57. warnings.filterwarnings(
  58. "ignore",
  59. category=DeprecationWarning,
  60. message="single_target_shortest_path_length will",
  61. )
  62. warnings.filterwarnings(
  63. "ignore",
  64. category=DeprecationWarning,
  65. message="shortest_path for all_pairs",
  66. )
  67. warnings.filterwarnings(
  68. "ignore", category=DeprecationWarning, message="\nforest_str is deprecated"
  69. )
  70. @pytest.fixture(autouse=True)
  71. def add_nx(doctest_namespace):
  72. doctest_namespace["nx"] = networkx
  73. # What dependencies are installed?
  74. try:
  75. import numpy
  76. has_numpy = True
  77. except ImportError:
  78. has_numpy = False
  79. try:
  80. import scipy
  81. has_scipy = True
  82. except ImportError:
  83. has_scipy = False
  84. try:
  85. import matplotlib
  86. has_matplotlib = True
  87. except ImportError:
  88. has_matplotlib = False
  89. try:
  90. import pandas
  91. has_pandas = True
  92. except ImportError:
  93. has_pandas = False
  94. try:
  95. import pygraphviz
  96. has_pygraphviz = True
  97. except ImportError:
  98. has_pygraphviz = False
  99. try:
  100. import pydot
  101. has_pydot = True
  102. except ImportError:
  103. has_pydot = False
  104. try:
  105. import sympy
  106. has_sympy = True
  107. except ImportError:
  108. has_sympy = False
  109. # List of files that pytest should ignore
  110. collect_ignore = []
  111. needs_numpy = [
  112. "algorithms/approximation/traveling_salesman.py",
  113. "algorithms/centrality/current_flow_closeness.py",
  114. "algorithms/node_classification.py",
  115. "algorithms/non_randomness.py",
  116. "algorithms/shortest_paths/dense.py",
  117. "linalg/bethehessianmatrix.py",
  118. "linalg/laplacianmatrix.py",
  119. "utils/misc.py",
  120. "algorithms/centrality/laplacian.py",
  121. ]
  122. needs_scipy = [
  123. "algorithms/approximation/traveling_salesman.py",
  124. "algorithms/assortativity/correlation.py",
  125. "algorithms/assortativity/mixing.py",
  126. "algorithms/assortativity/pairs.py",
  127. "algorithms/bipartite/matrix.py",
  128. "algorithms/bipartite/spectral.py",
  129. "algorithms/centrality/current_flow_betweenness.py",
  130. "algorithms/centrality/current_flow_betweenness_subset.py",
  131. "algorithms/centrality/eigenvector.py",
  132. "algorithms/centrality/katz.py",
  133. "algorithms/centrality/second_order.py",
  134. "algorithms/centrality/subgraph_alg.py",
  135. "algorithms/communicability_alg.py",
  136. "algorithms/link_analysis/hits_alg.py",
  137. "algorithms/link_analysis/pagerank_alg.py",
  138. "algorithms/node_classification.py",
  139. "algorithms/similarity.py",
  140. "convert_matrix.py",
  141. "drawing/layout.py",
  142. "generators/spectral_graph_forge.py",
  143. "linalg/algebraicconnectivity.py",
  144. "linalg/attrmatrix.py",
  145. "linalg/bethehessianmatrix.py",
  146. "linalg/graphmatrix.py",
  147. "linalg/modularitymatrix.py",
  148. "linalg/spectrum.py",
  149. "utils/rcm.py",
  150. "algorithms/centrality/laplacian.py",
  151. ]
  152. needs_matplotlib = ["drawing/nx_pylab.py"]
  153. needs_pandas = ["convert_matrix.py"]
  154. needs_pygraphviz = ["drawing/nx_agraph.py"]
  155. needs_pydot = ["drawing/nx_pydot.py"]
  156. needs_sympy = ["algorithms/polynomials.py"]
  157. if not has_numpy:
  158. collect_ignore += needs_numpy
  159. if not has_scipy:
  160. collect_ignore += needs_scipy
  161. if not has_matplotlib:
  162. collect_ignore += needs_matplotlib
  163. if not has_pandas:
  164. collect_ignore += needs_pandas
  165. if not has_pygraphviz:
  166. collect_ignore += needs_pygraphviz
  167. if not has_pydot:
  168. collect_ignore += needs_pydot
  169. if not has_sympy:
  170. collect_ignore += needs_sympy