Download presentation
Presentation is loading. Please wait.
1
Selenium HP Web Test Tool Training
Portnov Computer School Selenium HP Web Test Tool Training Test Automation For Web-Based Applications Presenter: Ellie Skobel
2
Day 1 Unittest Basics
3
Unittest A Python language version of JUnit Also referred to as PyUnit
xUnit frameworks are available for: ASP, C++, C#, Eiffel, Delphi, Perl, PHP, Python, REBOL, Smalltalk, and Visual Basic A Python language version of JUnit Also referred to as PyUnit Created in 1997 by Erich Gamma and Kent Beck Erich Gamma - well known as one of the “Gang of Four” who gave us the now classic Design Patterns book. Kent Beck is equally well known for his groundbreaking work in the software discipline known as Extreme Programming The underlying testing model (xUnit) is on its way to becoming the standard framework for any language.
4
Framework A semi-complete application.
Provides a reusable, common structure that can be shared between applications. Developers incorporate the framework into their own application and extend it to meet their specific needs. Differs from toolkits by providing a coherent structure, rather than a simple set of utility classes.
5
Unit Test Examines the behavior of a distinct unit of work
A unit of work is a task that is not directly dependent on the completion of any other task. In a Unit Testing framework: Each unit test must run independently of all other unit tests. Errors must be detected and reported test by test. It must be easy to define which unit tests will run.
6
Unittest Features test fixture test case test suite test runner
preparation needed to perform one or more tests, any associate cleanup test case smallest unit of testing. It checks for a specific response to a particular set of inputs unittest provides a base class, TestCase, which may be used to create new test cases test suite collection of test cases, test suites, or both used to aggregate tests that should be executed together test runner component which executes tests and reports the result.
7
Creating Test Cases A test case is created by sub-classing unittest.TestCase Individual tests are defined with method names that start with the letters 'test'. This naming convention informs the test runner about which methods represent tests. class AutoSuggest is a subclass of unittest.TestCase Name of a method which defines a testcase, starts with 'test'
8
setUp() and tearDown() methods are for preperation and cleanup
Writing a Test Cases The setUp() and tearDown() methods allow you to define instructions that will be executed before and after each test method. Individual tests are defined with method names that start with the letters 'test' Each test uses TestCase assert* methods to check for an expected result. assertEqual() assertTrue() assertFalse() etc. setUp() and tearDown() methods are for preperation and cleanup assert* methods are used to verify actual outcome against expected result
9
Assert Methods Method Checks that assertEqual(a, b) a == b
assertNotEqual(a, b) a != b assertTrue(x) bool(x) is True assertFalse(x) bool(x) is False assertIs(a, b) a is b assertIsNot(a, b) a is not b assertIsNone(x) x is None assertIsNotNone(x) x is not None assertIn(a, b) a in b assertNotIn(a, b) a not in b assertIsInstance(a, b) isinstance(a, b) assertNotIsInstance(a, b) not isinstance(a, b) assertRaises(exception) Method Checks that assertAlmostEqual(a, b) round(a-b, 7) == 0 assertNotAlmostEqual(a, b) round(a-b, 7) != 0 assertGreater(a, b) a > b assertGreaterEqual(a, b) a >= b assertLess(a, b) a < b assertLessEqual(a, b) a <= b assertRegexpMatches(s, r) r.search(s) assertNotRegexpMatches(s, r) not r.search(s) assertItemsEqual(a, b) sorted(a) == sorted(b) and works with unhashable objs assertDictContainsSubset(a, b) all the key/value pairs in a exist in b assertMultiLineEqual(a, b) strings assertSequenceEqual(a, b) sequences assertListEqual(a, b) lists assertTupleEqual(a, b) tuples assertSetEqual(a, b) sets or frozensets assertDictEqual(a, b) dicts
10
Definitions TestCase: class that extends the unittest.TestCase class.
Contains one or more tests represented by testXXX methods. Used to group together tests that exercise common behaviors. TestSuite - an aggregation of individual tests cases and test suites A convenient way to group together tests that are related.
11
Creating TestSuite Instantiate a new TestSuite object()
my_suite = unittest.TestSuite() Import your TestCase class from tests.MyTestCase import MyTestCase Add a test from the TestCase to your suite my_suite.addTest(MyTestCase("test_something")) Add all tests from the TestCase to a suite my_suite.addTest(unittest.makeSuite(MyTestCase)) Add another suite to your suite my_suite.addTest(another_suite) OR my_suite.addTest(MyTestSuite.suite())
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.