|
4 | 4 | "bytes" |
5 | 5 | "errors" |
6 | 6 | "flag" |
| 7 | + "fmt" |
7 | 8 | "io" |
8 | 9 | "math/rand" |
9 | 10 | "os" |
@@ -752,17 +753,83 @@ func TestUnInitializedSuites(t *testing.T) { |
752 | 753 | }) |
753 | 754 | } |
754 | 755 |
|
755 | | -var register = map[string]bool{"SuiteTesterTestTwo": true} |
| 756 | +// Test/Example of SkipTest |
756 | 757 |
|
757 | | -func skip(suiteName, testName string) bool { |
758 | | - return register[suiteName+testName] |
| 758 | +type buildVersion struct { |
| 759 | + major int |
| 760 | + minor int |
| 761 | +} |
| 762 | + |
| 763 | +func (v buildVersion) after(other buildVersion) bool { |
| 764 | + if v.major > other.major { |
| 765 | + return true |
| 766 | + } |
| 767 | + if v.major < other.major { |
| 768 | + return false |
| 769 | + } |
| 770 | + if v.minor > other.minor { |
| 771 | + return true |
| 772 | + } |
| 773 | + return false |
| 774 | +} |
| 775 | + |
| 776 | +type testRegistration struct { |
| 777 | + release buildVersion |
| 778 | + features []string |
| 779 | +} |
| 780 | + |
| 781 | +var testRegistrations = map[string]testRegistration{ |
| 782 | + "SuiteWithSkipTestOne": {buildVersion{1, 2}, []string{"myOldFeature"}}, |
| 783 | + "SuiteWithSkipTestTwo": {buildVersion{2, 0}, []string{"myNewFeature"}}, |
| 784 | +} |
| 785 | + |
| 786 | +func contains(s []string, v string) bool { |
| 787 | + for i := range s { |
| 788 | + if v == s[i] { |
| 789 | + return true |
| 790 | + } |
| 791 | + } |
| 792 | + return false |
| 793 | +} |
| 794 | + |
| 795 | +// reusing the Suite struct defined above |
| 796 | +type SuiteWithSkip struct { |
| 797 | + SuiteTester |
| 798 | +} |
| 799 | + |
| 800 | +// SkipTest Implements the SkipTest interface |
| 801 | +func (s *SuiteWithSkip) SkipTest(testSuiteName string, testName string) bool { |
| 802 | + testRegister, ok := testRegistrations[testSuiteName+testName] |
| 803 | + if !ok { |
| 804 | + return false |
| 805 | + } |
| 806 | + |
| 807 | + // runParameters. |
| 808 | + // These could for example be defined in a file, or as cli arguments. |
| 809 | + // In this example we define them here |
| 810 | + currentVersion := buildVersion{1, 2} |
| 811 | + enabledFeatures := []string{"myOldFeature", "myNewFeature"} |
| 812 | + |
| 813 | + if testRegister.release.after(currentVersion) { |
| 814 | + fmt.Printf("Skipping %s, due to release", testName) |
| 815 | + return true |
| 816 | + } |
| 817 | + |
| 818 | + for _, registeredFeature := range testRegister.features { |
| 819 | + if !contains(enabledFeatures, registeredFeature) { |
| 820 | + fmt.Printf("Skipping %s, due to feature", testName) |
| 821 | + return true |
| 822 | + } |
| 823 | + } |
| 824 | + |
| 825 | + return false |
759 | 826 | } |
760 | 827 |
|
761 | 828 | // TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we |
762 | 829 | // can run our suite using the Run(*testing.T, TestingSuite) function. |
763 | 830 | func TestRunSuiteWithSkip(t *testing.T) { |
764 | | - suiteTester := new(SuiteTester) |
765 | | - RunWithSkip(t, suiteTester, skip) |
| 831 | + suiteTester := new(SuiteWithSkip) |
| 832 | + Run(t, suiteTester) |
766 | 833 |
|
767 | 834 | // Normally, the test would end here. The following are simply |
768 | 835 | // some assertions to ensure that the Run function is working as |
@@ -795,11 +862,11 @@ func TestRunSuiteWithSkip(t *testing.T) { |
795 | 862 | assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkip/TestSubtest/second") |
796 | 863 |
|
797 | 864 | for _, suiteName := range suiteTester.SuiteNameAfter { |
798 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 865 | + assert.Equal(t, "SuiteWithSkip", suiteName) |
799 | 866 | } |
800 | 867 |
|
801 | 868 | for _, suiteName := range suiteTester.SuiteNameBefore { |
802 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 869 | + assert.Equal(t, "SuiteWithSkip", suiteName) |
803 | 870 | } |
804 | 871 |
|
805 | 872 | for _, when := range suiteTester.TimeAfter { |
@@ -839,11 +906,22 @@ func TestRunSuiteWithSkip(t *testing.T) { |
839 | 906 |
|
840 | 907 | } |
841 | 908 |
|
| 909 | +// SuiteWithSkipAll reuses the Suite struct defined above and implements the SkipTest interface skipping all tests |
| 910 | +type SuiteWithSkipAll struct { |
| 911 | + SuiteTester |
| 912 | +} |
| 913 | + |
| 914 | +// SkipTest Implements the SkipTest interface all tests and Setup/TeardownSuite will be skipped |
| 915 | +func (s *SuiteWithSkipAll) SkipTest(testSuiteName string, testName string) bool { |
| 916 | + return true |
| 917 | +} |
| 918 | + |
842 | 919 | // TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we |
843 | 920 | // can run our suite using the Run(*testing.T, TestingSuite) function. |
| 921 | + |
844 | 922 | func TestRunSuiteWithSkipAll(t *testing.T) { |
845 | | - suiteTester := new(SuiteTester) |
846 | | - RunWithSkip(t, suiteTester, func(_, _ string) bool { return true }) |
| 923 | + suiteTester := new(SuiteWithSkipAll) |
| 924 | + Run(t, suiteTester) |
847 | 925 |
|
848 | 926 | // Normally, the test would end here. The following are simply |
849 | 927 | // some assertions to ensure that the Run function is working as |
@@ -876,11 +954,11 @@ func TestRunSuiteWithSkipAll(t *testing.T) { |
876 | 954 | assert.NotContains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkipAll/TestSubtest/second") |
877 | 955 |
|
878 | 956 | for _, suiteName := range suiteTester.SuiteNameAfter { |
879 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 957 | + assert.Equal(t, "SuiteWithSkipAll", suiteName) |
880 | 958 | } |
881 | 959 |
|
882 | 960 | for _, suiteName := range suiteTester.SuiteNameBefore { |
883 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 961 | + assert.Equal(t, "SuiteWithSkipAll", suiteName) |
884 | 962 | } |
885 | 963 |
|
886 | 964 | for _, when := range suiteTester.TimeAfter { |
|
0 commit comments