bs4.tests.test_tree#

Tests for Beautiful Soup’s tree traversal methods.

The tree traversal methods are the main advantage of using Beautiful Soup over just using a parser.

Different parsers will build different Beautiful Soup trees given the same markup, but all Beautiful Soup trees can be traversed with the methods tested here.

Module Contents#

Classes#

TestFind

Basic tests of the find() method.

TestFindAll

Basic tests of the find_all() method.

TestFindAllBasicNamespaces

TestFindAllByName

Test ways of finding tags by tag name.

TestFindAllByAttribute

TestSmooth

Test Tag.smooth.

TestIndex

Test Tag.index

TestParentOperations

Test navigation and searching through an element's parents.

ProximityTest

TestNextOperations

TestPreviousOperations

SiblingTest

TestNextSibling

TestPreviousSibling

TestTreeModification

TestDeprecatedArguments

class bs4.tests.test_tree.TestFind#

Bases: bs4.tests.SoupTest

Basic tests of the find() method.

find() just calls find_all() with limit=1, so it’s not tested all that thouroughly here.

test_find_tag()#
test_unicode_text_find()#
test_unicode_attribute_find()#
test_find_everything()#

Test an optimization that finds all tags.

test_find_everything_with_name()#

Test an optimization that finds all tags with a given name.

class bs4.tests.test_tree.TestFindAll#

Bases: bs4.tests.SoupTest

Basic tests of the find_all() method.

test_find_all_text_nodes()#

You can search the tree for text nodes.

test_find_all_limit()#

You can limit the number of items returned by find_all.

test_calling_a_tag_is_calling_findall()#
test_find_all_with_self_referential_data_structure_does_not_cause_infinite_recursion()#
test_find_all_resultset()#

All find_all calls return a ResultSet

class bs4.tests.test_tree.TestFindAllBasicNamespaces#

Bases: bs4.tests.SoupTest

test_find_by_namespaced_name()#
class bs4.tests.test_tree.TestFindAllByName#

Bases: bs4.tests.SoupTest

Test ways of finding tags by tag name.

setup_method()#
test_find_all_by_tag_name()#
test_find_all_by_name_and_text()#
test_find_all_on_non_root_element()#
test_calling_element_invokes_find_all()#
test_find_all_by_tag_strainer()#
test_find_all_by_tag_names()#
test_find_all_by_tag_dict()#
test_find_all_by_tag_re()#
test_find_all_with_tags_matching_method()#
test_find_with_multi_valued_attribute()#
class bs4.tests.test_tree.TestFindAllByAttribute#

Bases: bs4.tests.SoupTest

test_find_all_by_attribute_name()#
test_find_all_by_utf8_attribute_value()#
test_find_all_by_attribute_dict()#
test_find_all_by_class()#
test_find_by_class_when_multiple_classes_present()#
test_find_all_with_non_dictionary_for_attrs_finds_by_class()#
test_find_all_with_string_for_attrs_finds_multiple_classes()#
test_find_all_by_attribute_soupstrainer()#
test_find_all_with_missing_attribute()#
test_find_all_with_defined_attribute()#
test_find_all_with_numeric_attribute()#
test_find_all_with_list_attribute_values()#
test_find_all_with_regular_expression_attribute_value()#
test_find_by_name_and_containing_string()#
test_find_by_name_and_containing_string_when_string_is_buried()#
test_find_by_attribute_and_containing_string()#
class bs4.tests.test_tree.TestSmooth#

Bases: bs4.tests.SoupTest

Test Tag.smooth.

test_smooth()#
class bs4.tests.test_tree.TestIndex#

Bases: bs4.tests.SoupTest

Test Tag.index

test_index()#
class bs4.tests.test_tree.TestParentOperations#

Bases: bs4.tests.SoupTest

Test navigation and searching through an element’s parents.

setup_method()#
test_parent()#
test_parent_of_top_tag_is_soup_object()#
test_soup_object_has_no_parent()#
test_find_parents()#
test_find_parent()#
test_parent_of_text_element()#
test_text_element_find_parent()#
test_parent_generator()#
class bs4.tests.test_tree.ProximityTest#

Bases: bs4.tests.SoupTest

setup_method()#
class bs4.tests.test_tree.TestNextOperations#

Bases: ProximityTest

setup_method()#
test_next()#
test_next_of_last_item_is_none()#
test_next_of_root_is_none()#
test_find_all_next()#
test_find_next()#
test_find_next_for_text_element()#
test_next_generator()#
class bs4.tests.test_tree.TestPreviousOperations#

Bases: ProximityTest

setup_method()#
test_previous()#
test_previous_of_first_item_is_none()#
test_previous_of_root_is_none()#
test_find_all_previous()#
test_find_previous()#
test_find_previous_for_text_element()#
test_previous_generator()#
class bs4.tests.test_tree.SiblingTest#

Bases: bs4.tests.SoupTest

setup_method()#
class bs4.tests.test_tree.TestNextSibling#

Bases: SiblingTest

setup_method()#
test_next_sibling_of_root_is_none()#
test_next_sibling()#
test_next_sibling_may_not_exist()#
test_find_next_sibling()#
test_next_siblings()#
test_next_sibling_for_text_element()#
class bs4.tests.test_tree.TestPreviousSibling#

Bases: SiblingTest

setup_method()#
test_previous_sibling_of_root_is_none()#
test_previous_sibling()#
test_previous_sibling_may_not_exist()#
test_find_previous_sibling()#
test_previous_siblings()#
test_previous_sibling_for_text_element()#
class bs4.tests.test_tree.TestTreeModification#

Bases: bs4.tests.SoupTest

test_attribute_modification()#
test_new_tag_creation()#
test_append_to_contents_moves_tag()#
test_replace_with_returns_thing_that_was_replaced()#
test_unwrap_returns_thing_that_was_replaced()#
test_replace_with_and_unwrap_give_useful_exception_when_tag_has_no_parent()#
test_replace_tag_with_itself()#
test_replace_tag_with_its_parent_raises_exception()#
test_insert_tag_into_itself_raises_exception()#
test_insert_beautifulsoup_object_inserts_children()#

Inserting one BeautifulSoup object into another actually inserts all of its children – you’ll never combine BeautifulSoup objects.

test_replace_with_maintains_next_element_throughout()#
test_replace_final_node()#
test_consecutive_text_nodes()#
test_insert_string()#
test_insert_tag()#
test_append_child_thats_already_at_the_end()#
test_extend()#
test_extend_with_another_tags_contents(get_tags)#
test_move_tag_to_beginning_of_parent()#
test_insert_works_on_empty_element_tag()#
test_insert_before()#
test_insert_multiple_before()#
test_insert_after()#
test_insert_multiple_after()#
test_insert_after_raises_exception_if_after_has_no_meaning()#
test_insert_before_raises_notimplementederror_if_before_has_no_meaning()#
test_replace_with()#
test_replace_with_errors()#
test_replace_with_multiple()#
test_replace_first_child()#
test_replace_last_child()#
test_nested_tag_replace_with()#
test_unwrap()#
test_wrap()#
test_wrap_extracts_tag_from_elsewhere()#
test_wrap_puts_new_contents_at_the_end()#
test_extract()#
test_extract_distinguishes_between_identical_strings()#
test_extract_multiples_of_same_tag()#
test_extract_works_when_element_is_surrounded_by_identical_strings()#
test_clear()#

Tag.clear()

test_decompose()#
test_string_set()#

Tag.string = ‘string’

test_string_set_does_not_affect_original_string()#
test_set_string_preserves_class_of_string()#
class bs4.tests.test_tree.TestDeprecatedArguments#

Bases: bs4.tests.SoupTest

test_find_type_method_string(method_name)#
test_soupstrainer_constructor_string()#