Selenium2+python Automatic 56-unittest Assertion (ASSERT) "Reprint"

Source: Internet
Author: User
Tags assert

Objective

In the test case, after the test case is executed, the final step is to determine whether the test result is pass or fail, and the automated test script typically calls this method of generating the test result as an assertion (assert).

When using the UnitTest component to test the use case, the assertion method is still many, the following describes several commonly used assertion methods: Assertequal, Assertin, asserttrue

First, simple case

1. The following 4 case is written, where the fourth one is the execution of the failed

# Coding:utf-8
Import UnitTest
Class Test (UnitTest. TestCase):
def test01 (self):
"' Judge A = = B '"
A = 1
b = 1
Self.assertequal (A, B)

def test02 (self):
"' Judge A in B '
A = "Hello"
b = "Hello world!"
Self.assertin (A, B)

def test03 (self):
"Judge A is True"
A = True
Self.asserttrue (a)

def test04 (self):
"Failure Case"
A = "Shanghai-leisurely"
b = "Yoyo"
Self.assertequal (A, B)

if __name__ = = "__main__":
Unittest.main ()

2. The results of the implementation are as follows

Failure
Expected: ' \xe4\xb8\x8a\xe6\xb5\xb7-\xe6\x82\xa0\xe6\x82\xa0 '
Actual: ' Yoyo '
<click to see difference>

Traceback (most recent):
File "D:\test\yoyotest\kecheng\test12.py", line +, in test04
Self.assertequal (A, B)
Assertionerror: ' \xe4\xb8\x8a\xe6\xb5\xb7-\xe6\x82\xa0\xe6\x82\xa0 '! = ' yoyo '
3. The result of the implementation, the Chinese code is not correct, not normal display Chinese, in this case, you can customize the exception output

Ii.. Custom exceptions

1. Take assertequal as an example to analyze:

Assertequal (self, first, second, msg=none)
Fail if the objects is unequal as determined by the ' = = '
operator.

2. Translation: If two objects cannot be equal, the return fails, equivalent to Return:first==second

3. In addition to the comparison of the two parameters first and second, there is a third parameter msg=none, this msg parameter is encountered after the exception of the custom output information

Third, the unittest commonly used assertion method

1.assertEqual (self, first, second, msg=none)

--Determine the equality of two parameters: first = = Second

2.assertNotEqual (self, first, second, msg=none)

--Determine the two parameters are unequal: first! = Second

3.assertIn (self, Member, container, Msg=none)

--The judgment is whether the string contains: Member in container

4.assertNotIn (self, Member, container, Msg=none)

--The judgment is whether the string does not contain: Member not in container

5.assertTrue (self, expr, msg=none)

--Determine if true: expr is true

6.assertFalse (self, expr, msg=none)

--Determine if False: expr is false

7.assertIsNone (self, obj, msg=none)

--Determine if None:obj is None

8.assertIsNotNone (self, obj, msg=none)
--Determine if none:obj is not none



Iv. UnitTest All assertion methods

1. Here are all the assertion methods supported by the UnitTest framework, and interested students can look at it slowly.

| Assertalmostequal (self, first, second, Places=none, Msg=none, Delta=none)
| Fail if the objects is unequal as determined by their
| Difference rounded to the given number of decimal places
| (default 7) and comparing to zero, or by comparing the
| Between the objects is more than the given delta.
|
| Note that decimal places (from zero) is usually not the same
| As significant digits (measured from the most signficant digit).
|
| If the objects compare equal then they'll automatically
| Compare almost equal.
|
| Assertalmostequals = Assertalmostequal (self, first, second, Places=none, Msg=none, Delta=none)
|
| Assertdictcontainssubset (self, expected, actual, Msg=none)
| Checks whether actual is a superset of expected.
|
| Assertdictequal (self, D1, D2, Msg=none)
|
| Assertequal (self, first, second, msg=none)
| Fail if the objects is unequal as determined by the ' = = '
| operator.
|
| Assertequals = Assertequal (self, first, second, msg=none)
|
| Assertfalse (self, expr, msg=none)
| Check the expression is false.
|
| Assertgreater (self, A, B, Msg=none)
| Just like Self.asserttrue (a > B), but with a nicer default message.
|
| Assertgreaterequal (self, A, B, Msg=none)
| Just like Self.asserttrue (a >= b), but with a nicer default message.
|
| Assertin (self, Member, container, Msg=none)
| Just like Self.asserttrue (A-B), but with a nicer default message.
|
| Assertis (self, expr1, expr2, Msg=none)
| Just like Self.asserttrue (A was B), but with a nicer default message.
|
| Assertisinstance (self, obj, CLS, Msg=none)
| Same as Self.asserttrue (Isinstance (obj, CLS)), with a nicer
| Default message.
|
| Assertisnone (self, obj, msg=none)
| Same as Self.asserttrue (obj is None), with a nicer default message.
|
| Assertisnot (self, expr1, expr2, Msg=none)
| Just like Self.asserttrue (A was not B), but with a nicer default message.
|
| Assertisnotnone (self, obj, msg=none)
| Included for symmetry with Assertisnone.
|
| Assertitemsequal (self, expected_seq, Actual_seq, Msg=none)
| An unordered sequence specific comparison. It asserts that
| Actual_seq and expected_seq have the same element counts.
| Equivalent to::
|
| Self.assertequal (Counter (ITER (ACTUAL_SEQ)),
| Counter (ITER (EXPECTED_SEQ)))
|
| Asserts that all element has the same count in both sequences.
| Example:
| -[0, 1, 1] and [1, 0, 1] compare equal.
| -[0, 0, 1] and [0, 1] compare unequal.
|
| Assertless (self, A, B, Msg=none)
| Just like Self.asserttrue (a < b), but with a nicer default message.
|
| Assertlessequal (self, A, B, Msg=none)
| Just like Self.asserttrue (a <= b), but with a nicer default message.
|
| Assertlistequal (self, list1, List2, Msg=none)
| A list-specific Equality assertion.
|
| Args:
| List1:the first list to compare.
| List2:the second list to compare.
| Msg:optional message to use on failure instead of a list of
| Differences.
|
| Assertmultilineequal (self, first, second, msg=none)
| Assert that multi-line strings is equal.
|
| Assertnotalmostequal (self, first, second, Places=none, Msg=none, Delta=none)
| Fail if the objects is equal as determined by their
| Difference rounded to the given number of decimal places
| (default 7) and comparing to zero, or by comparing the
| Between the objects is less than the given delta.
|
| Note that decimal places (from zero) is usually not the same
| As significant digits (measured from the most signficant digit).
|
| Objects that is equal automatically fail.
|
| Assertnotalmostequals = Assertnotalmostequal (self, first, second, Places=none, Msg=none, Delta=none)
|
| Assertnotequal (self, first, second, msg=none)
| Fail if the objects is equal as determined by the '! = '
| operator.
|
| Assertnotequals = Assertnotequal (self, first, second, msg=none)
|
| Assertnotin (self, Member, container, Msg=none)
| Just like Self.asserttrue (a No in B), but with a nicer default message.
|
| Assertnotisinstance (self, obj, CLS, Msg=none)
| Included for symmetry with Assertisinstance.
|
| Assertnotregexpmatches (self, text, unexpected_regexp, Msg=none)
| Fail the test if the text matches the regular expression.
|
| Assertraises (self, excclass, Callableobj=none, *args, **kwargs)
| Fail unless an exception of class Excclass is raised
| By Callableobj when invoked with arguments args and keyword
| Arguments Kwargs. If a different type of exception is
| Raised, it'll is not being caught, and the test case would be
| Deemed to has suffered an error, exactly as for an
| Unexpected exception.
|
| If called with callableobj omitted or None, would return a
| Context object used like this::
|
| With Self.assertraises (someexception):
| Do_something ()
|
| The context manager keeps a reference to the exception as
| The ' exception ' attribute. This allows inspect the
| Exception after the assertion::
|
| With Self.assertraises (someexception) as CM:
| Do_something ()
| The_exception = Cm.exception
| Self.assertequal (The_exception.error_code, 3)
|
| Assertraisesregexp (self, expected_exception, Expected_regexp, Callable_obj=none, *args, **kwargs)
| Asserts the message in a raised exception matches a regexp.
|
| Args:
| Expected_exception:exception class expected to be raised.
| Expected_regexp:regexp (re-pattern object or string) expected
| To is found in error message.
| Callable_obj:function to be called.
| Args:extra args.
| Kwargs:extra Kwargs.
|
| Assertregexpmatches (self, text, expected_regexp, Msg=none)
| Fail the test unless the text matches the regular expression.
|
| Assertsequenceequal (self, seq1, SEQ2, Msg=none, Seq_type=none)
| An equality assertion for ordered sequences (like lists and tuples).
|
| For the purposes of this function, a valid ordered sequence type is one
| which can be indexed, have a length, and have an equality operator.
|
| Args:
| Seq1:the first sequence to compare.
| Seq2:the second sequence to compare.
| Seq_type:the expected datatype of the sequences, or None if no
| DataType should be enforced.
| Msg:optional message to use on failure instead of a list of
| Differences.
|
| Assertsetequal (self, Set1, Set2, Msg=none)
| A set-specific Equality assertion.
|
| Args:
| Set1:the first set to compare.
| Set2:the second set to compare.
| Msg:optional message to use on failure instead of a list of
| Differences.
|
| Assertsetequal uses ducktyping to support different types of sets, and
| is optimized for sets specifically (parameters must support a
| Difference method).
|
| Asserttrue (self, expr, msg=none)
| Check the expression is true.
|
| Asserttupleequal (self, tuple1, Tuple2, Msg=none)
| A tuple-specific Equality assertion.
|
| Args:
| Tuple1:the first tuple to compare.
| tuple2:the second tuple to compare.
| Msg:optional message to use on failure instead of a list of
| Differences.

Selenium2+python Automatic 56-unittest Assertion (ASSERT) "Reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.